Kernel-Objects.st 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. Smalltalk createPackage: 'Kernel-Objects'!
  2. nil subclass: #ProtoObject
  3. instanceVariableNames: ''
  4. package: 'Kernel-Objects'!
  5. !ProtoObject commentStamp!
  6. I implement the basic behavior required for any object in Amber.
  7. In most cases, subclassing `ProtoObject` is wrong and `Object` should be used instead. However subclassing `ProtoObject` can be useful in some special cases like proxy implementations.!
  8. !ProtoObject methodsFor: 'accessing'!
  9. class
  10. <return self.klass>
  11. !
  12. identityHash
  13. <
  14. var hash=self.identityHash;
  15. if (hash) return hash;
  16. hash=$core.nextId();
  17. Object.defineProperty(self, 'identityHash', {value:hash});
  18. return hash;
  19. >
  20. !
  21. instVarAt: aString
  22. < return self['@'+aString] >
  23. !
  24. instVarAt: aString put: anObject
  25. < self['@' + aString] = anObject >
  26. !
  27. yourself
  28. ^ self
  29. ! !
  30. !ProtoObject methodsFor: 'comparing'!
  31. = anObject
  32. ^ self == anObject
  33. !
  34. == anObject
  35. <return self._class() === $recv(anObject)._class() && self._isSameInstanceAs_(anObject)>
  36. !
  37. isSameInstanceAs: anObject
  38. ^ self identityHash = anObject identityHash
  39. !
  40. ~= anObject
  41. ^ (self = anObject) = false
  42. !
  43. ~~ anObject
  44. ^ (self == anObject) = false
  45. ! !
  46. !ProtoObject methodsFor: 'converting'!
  47. asString
  48. ^ self printString
  49. ! !
  50. !ProtoObject methodsFor: 'error handling'!
  51. doesNotUnderstand: aMessage
  52. MessageNotUnderstood new
  53. receiver: self;
  54. message: aMessage;
  55. signal
  56. ! !
  57. !ProtoObject methodsFor: 'evaluating'!
  58. evaluate: aString on: anEvaluator
  59. ^ anEvaluator evaluate: aString receiver: self
  60. ! !
  61. !ProtoObject methodsFor: 'initialization'!
  62. initialize
  63. ! !
  64. !ProtoObject methodsFor: 'inspecting'!
  65. inspect
  66. Inspector inspect: self
  67. !
  68. inspectOn: anInspector
  69. ! !
  70. !ProtoObject methodsFor: 'message handling'!
  71. perform: aString
  72. ^ self perform: aString withArguments: #()
  73. !
  74. perform: aString withArguments: aCollection
  75. <return $core.send(self, aString._asJavaScriptMethodName(), aCollection)>
  76. ! !
  77. !ProtoObject methodsFor: 'printing'!
  78. printOn: aStream
  79. aStream nextPutAll: (self class name first isVowel
  80. ifTrue: [ 'an ' ]
  81. ifFalse: [ 'a ' ]).
  82. aStream nextPutAll: self class name
  83. !
  84. printString
  85. ^ String streamContents: [ :str |
  86. self printOn: str ]
  87. ! !
  88. !ProtoObject methodsFor: 'testing'!
  89. ifNil: aBlock
  90. "inlined in the Compiler"
  91. ^ self
  92. !
  93. ifNil: aBlock ifNotNil: anotherBlock
  94. "inlined in the Compiler"
  95. ^ anotherBlock value: self
  96. !
  97. ifNotNil: aBlock
  98. "inlined in the Compiler"
  99. ^ aBlock value: self
  100. !
  101. ifNotNil: aBlock ifNil: anotherBlock
  102. "inlined in the Compiler"
  103. ^ aBlock value: self
  104. !
  105. isKindOf: aClass
  106. ^ (self isMemberOf: aClass)
  107. ifTrue: [ true ]
  108. ifFalse: [ self class inheritsFrom: aClass ]
  109. !
  110. isNil
  111. ^ false
  112. !
  113. notNil
  114. ^ self isNil not
  115. ! !
  116. !ProtoObject class methodsFor: 'initialization'!
  117. initialize
  118. ! !
  119. ProtoObject subclass: #Object
  120. instanceVariableNames: ''
  121. package: 'Kernel-Objects'!
  122. !Object commentStamp!
  123. **I am the root of the Smalltalk class system**. With the exception of unual subclasses of `ProtoObject`, all other classes in the system are subclasses of me.
  124. I provide default behavior common to all normal objects (some of it inherited from `ProtoObject`), such as:
  125. - accessing
  126. - copying
  127. - comparison
  128. - error handling
  129. - message sending
  130. - reflection
  131. Also utility messages that all objects should respond to are defined here.
  132. I have no instance variable.
  133. ##Access
  134. Instance variables can be accessed with `#instVarAt:` and `#instVarAt:put:`. `#instanceVariableNames` answers a collection of all instance variable names.
  135. Accessing JavaScript properties of an object is done through `#basicAt:`, `#basicAt:put:` and `basicDelete:`.
  136. ##Copying
  137. Copying an object is handled by `#copy` and `#deepCopy`. The first one performs a shallow copy of the receiver, while the second one performs a deep copy.
  138. The hook method `#postCopy` can be overriden in subclasses to copy fields as necessary to complete the full copy. It will be sent by the copy of the receiver.
  139. ##Comparison
  140. I understand equality `#=` and identity `#==` comparison.
  141. ##Error handling
  142. - `#halt` is the typical message to use for inserting breakpoints during debugging.
  143. - `#error:` throws a generic error exception
  144. - `#doesNotUnderstand:` handles the fact that there was an attempt to send the given message to the receiver but the receiver does not understand this message.
  145. Overriding this message can be useful to implement proxies for example.!
  146. !Object methodsFor: 'accessing'!
  147. basicAt: aString
  148. <return self[aString]>
  149. !
  150. basicAt: aString put: anObject
  151. <return self[aString] = anObject>
  152. !
  153. basicDelete: aString
  154. <delete self[aString]; return aString>
  155. !
  156. size
  157. self error: 'Object not indexable'
  158. ! !
  159. !Object methodsFor: 'browsing'!
  160. browse
  161. Finder findClass: self class
  162. ! !
  163. !Object methodsFor: 'converting'!
  164. -> anObject
  165. ^ Association key: self value: anObject
  166. !
  167. asJSON
  168. | variables |
  169. variables := HashedCollection new.
  170. self class allInstanceVariableNames do: [ :each |
  171. variables at: each put: (self instVarAt: each) asJSON ].
  172. ^ variables
  173. !
  174. asJSONString
  175. ^ JSON stringify: self asJSON
  176. !
  177. asJavascript
  178. ^ self asString
  179. ! !
  180. !Object methodsFor: 'copying'!
  181. copy
  182. ^ self shallowCopy postCopy
  183. !
  184. deepCopy
  185. <
  186. var copy = self.klass._new();
  187. Object.keys(self).forEach(function (i) {
  188. if(/^@.+/.test(i)) {
  189. copy[i] = self[i]._deepCopy();
  190. }
  191. });
  192. return copy;
  193. >
  194. !
  195. postCopy
  196. !
  197. shallowCopy
  198. <
  199. var copy = self.klass._new();
  200. Object.keys(self).forEach(function(i) {
  201. if(/^@.+/.test(i)) {
  202. copy[i] = self[i];
  203. }
  204. });
  205. return copy;
  206. >
  207. ! !
  208. !Object methodsFor: 'error handling'!
  209. deprecatedAPI
  210. "Just a simple way to deprecate methods.
  211. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  212. but it could in the future."
  213. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'.
  214. !
  215. deprecatedAPI: aString
  216. "Just a simple way to deprecate methods.
  217. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  218. but it could in the future."
  219. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'.
  220. console warn: aString
  221. !
  222. error: aString
  223. Error signal: aString
  224. !
  225. halt
  226. Halt signal
  227. !
  228. shouldNotImplement
  229. self error: 'This method should not be implemented in ', self class name
  230. !
  231. subclassResponsibility
  232. self error: 'This method is a responsibility of a subclass'
  233. !
  234. throw: anObject
  235. < throw anObject >
  236. ! !
  237. !Object methodsFor: 'evaluating'!
  238. in: aValuable
  239. ^ aValuable value: self
  240. !
  241. value
  242. <return self.valueOf()>
  243. ! !
  244. !Object methodsFor: 'message handling'!
  245. basicPerform: aString
  246. ^ self basicPerform: aString withArguments: #()
  247. !
  248. basicPerform: aString withArguments: aCollection
  249. <return self[aString].apply(self, aCollection);>
  250. ! !
  251. !Object methodsFor: 'streaming'!
  252. putOn: aStream
  253. aStream nextPut: self
  254. ! !
  255. !Object methodsFor: 'testing'!
  256. isBehavior
  257. ^ false
  258. !
  259. isBoolean
  260. ^ false
  261. !
  262. isClass
  263. ^ false
  264. !
  265. isCompiledMethod
  266. ^ false
  267. !
  268. isImmutable
  269. ^ false
  270. !
  271. isMemberOf: aClass
  272. ^ self class = aClass
  273. !
  274. isMetaclass
  275. ^ false
  276. !
  277. isNumber
  278. ^ false
  279. !
  280. isPackage
  281. ^ false
  282. !
  283. isParseFailure
  284. ^ false
  285. !
  286. isString
  287. ^ false
  288. !
  289. isSymbol
  290. ^ false
  291. !
  292. respondsTo: aSelector
  293. ^ self class canUnderstand: aSelector
  294. ! !
  295. !Object class methodsFor: 'helios'!
  296. accessorProtocolWith: aGenerator
  297. aGenerator accessorProtocolForObject
  298. !
  299. accessorsSourceCodesWith: aGenerator
  300. aGenerator accessorsForObject
  301. !
  302. initializeProtocolWith: aGenerator
  303. aGenerator initializeProtocolForObject
  304. !
  305. initializeSourceCodesWith: aGenerator
  306. aGenerator initializeForObject
  307. ! !
  308. !Object class methodsFor: 'initialization'!
  309. initialize
  310. "no op"
  311. ! !
  312. Object subclass: #Boolean
  313. instanceVariableNames: ''
  314. package: 'Kernel-Objects'!
  315. !Boolean commentStamp!
  316. I define the protocol for logic testing operations and conditional control structures for the logical values (see the `controlling` protocol).
  317. I have two instances, `true` and `false`.
  318. I am directly mapped to JavaScript Boolean. The `true` and `false` objects are the JavaScript boolean objects.
  319. ## Usage Example:
  320. aBoolean not ifTrue: [ ... ] ifFalse: [ ... ]!
  321. !Boolean methodsFor: 'comparing'!
  322. == aBoolean
  323. <return aBoolean !!= null && self.valueOf() === (typeof aBoolean === "boolean" ? aBoolean : aBoolean.valueOf())>
  324. ! !
  325. !Boolean methodsFor: 'controlling'!
  326. & aBoolean
  327. <
  328. if(self == true) {
  329. return aBoolean;
  330. } else {
  331. return false;
  332. }
  333. >
  334. !
  335. and: aBlock
  336. ^ self = true
  337. ifTrue: aBlock
  338. ifFalse: [ false ]
  339. !
  340. ifFalse: aBlock
  341. "inlined in the Compiler"
  342. ^ self ifTrue: [] ifFalse: aBlock
  343. !
  344. ifFalse: aBlock ifTrue: anotherBlock
  345. "inlined in the Compiler"
  346. ^ self ifTrue: anotherBlock ifFalse: aBlock
  347. !
  348. ifTrue: aBlock
  349. "inlined in the Compiler"
  350. ^ self ifTrue: aBlock ifFalse: []
  351. !
  352. ifTrue: aBlock ifFalse: anotherBlock
  353. "inlined in the Compiler"
  354. <
  355. if(self == true) {
  356. return aBlock._value();
  357. } else {
  358. return anotherBlock._value();
  359. }
  360. >
  361. !
  362. not
  363. ^ self = false
  364. !
  365. or: aBlock
  366. ^ self = true
  367. ifTrue: [ true ]
  368. ifFalse: aBlock
  369. !
  370. | aBoolean
  371. <
  372. if(self == true) {
  373. return true;
  374. } else {
  375. return aBoolean;
  376. }
  377. >
  378. ! !
  379. !Boolean methodsFor: 'converting'!
  380. asBit
  381. ^ self ifTrue: [ 1 ] ifFalse: [ 0 ]
  382. !
  383. asJSON
  384. ^ self
  385. !
  386. asString
  387. < return self.toString() >
  388. ! !
  389. !Boolean methodsFor: 'copying'!
  390. deepCopy
  391. ^ self
  392. !
  393. shallowCopy
  394. ^ self
  395. ! !
  396. !Boolean methodsFor: 'printing'!
  397. printOn: aStream
  398. aStream nextPutAll: self asString
  399. ! !
  400. !Boolean methodsFor: 'testing'!
  401. isBoolean
  402. ^ true
  403. !
  404. isImmutable
  405. ^ true
  406. ! !
  407. Object subclass: #Date
  408. instanceVariableNames: ''
  409. package: 'Kernel-Objects'!
  410. !Date commentStamp!
  411. I am used to work with both dates and times. Therefore `Date today` and `Date now` are both valid in
  412. Amber and answer the same date object.
  413. Date directly maps to the `Date()` JavaScript constructor, and Amber date objects are JavaScript date objects.
  414. ## API
  415. The class-side `instance creation` protocol contains some convenience methods for creating date/time objects such as `#fromSeconds:`.
  416. Arithmetic and comparison is supported (see the `comparing` and `arithmetic` protocols).
  417. The `converting` protocol provides convenience methods for various convertions (to numbers, strings, etc.).!
  418. !Date methodsFor: 'accessing'!
  419. day
  420. ^ self dayOfWeek
  421. !
  422. day: aNumber
  423. self dayOfWeek: aNumber
  424. !
  425. dayOfMonth
  426. <return self.getDate()>
  427. !
  428. dayOfMonth: aNumber
  429. <self.setDate(aNumber)>
  430. !
  431. dayOfWeek
  432. <return self.getDay() + 1>
  433. !
  434. dayOfWeek: aNumber
  435. <return self.setDay(aNumber - 1)>
  436. !
  437. hours
  438. <return self.getHours()>
  439. !
  440. hours: aNumber
  441. <self.setHours(aNumber)>
  442. !
  443. milliseconds
  444. <return self.getMilliseconds()>
  445. !
  446. milliseconds: aNumber
  447. <self.setMilliseconds(aNumber)>
  448. !
  449. minutes
  450. <return self.getMinutes()>
  451. !
  452. minutes: aNumber
  453. <self.setMinutes(aNumber)>
  454. !
  455. month
  456. <return self.getMonth() + 1>
  457. !
  458. month: aNumber
  459. <self.setMonth(aNumber - 1)>
  460. !
  461. seconds
  462. <return self.getSeconds()>
  463. !
  464. seconds: aNumber
  465. <self.setSeconds(aNumber)>
  466. !
  467. time
  468. <return self.getTime()>
  469. !
  470. time: aNumber
  471. <self.setTime(aNumber)>
  472. !
  473. year
  474. <return self.getFullYear()>
  475. !
  476. year: aNumber
  477. <self.setFullYear(aNumber)>
  478. ! !
  479. !Date methodsFor: 'arithmetic'!
  480. + aDate
  481. <return self + aDate>
  482. !
  483. - aDate
  484. <return self - aDate>
  485. ! !
  486. !Date methodsFor: 'comparing'!
  487. < aDate
  488. <return self < aDate>
  489. !
  490. <= aDate
  491. <return self <= aDate>
  492. !
  493. = aDate
  494. ^ (aDate class == self class) and: [ self asMilliseconds == aDate asMilliseconds ]
  495. !
  496. > aDate
  497. <return self >> aDate>
  498. !
  499. >= aDate
  500. <return self >>= aDate>
  501. ! !
  502. !Date methodsFor: 'converting'!
  503. asDateString
  504. <return self.toDateString()>
  505. !
  506. asLocaleString
  507. <return self.toLocaleString()>
  508. !
  509. asMilliseconds
  510. ^ self time
  511. !
  512. asNumber
  513. ^ self asMilliseconds
  514. !
  515. asString
  516. <return self.toString()>
  517. !
  518. asTimeString
  519. <return self.toTimeString()>
  520. ! !
  521. !Date methodsFor: 'printing'!
  522. printOn: aStream
  523. aStream nextPutAll: self asString
  524. ! !
  525. !Date class methodsFor: 'accessing'!
  526. classTag
  527. "Returns a tag or general category for this class.
  528. Typically used to help tools do some reflection.
  529. Helios, for example, uses this to decide what icon the class should display."
  530. ^ 'magnitude'
  531. ! !
  532. !Date class methodsFor: 'instance creation'!
  533. fromMilliseconds: aNumber
  534. ^ self new: aNumber
  535. !
  536. fromSeconds: aNumber
  537. ^ self fromMilliseconds: aNumber * 1000
  538. !
  539. fromString: aString
  540. "Example: Date fromString('2011/04/15 00:00:00')"
  541. ^ self new: aString
  542. !
  543. millisecondsToRun: aBlock
  544. | t |
  545. t := Date now.
  546. aBlock value.
  547. ^ Date now - t
  548. !
  549. new: anObject
  550. <return new Date(anObject)>
  551. !
  552. now
  553. ^ self today
  554. !
  555. today
  556. ^ self new
  557. ! !
  558. Object subclass: #Number
  559. instanceVariableNames: ''
  560. package: 'Kernel-Objects'!
  561. !Number commentStamp!
  562. I am the Amber representation for all numbers.
  563. I am directly mapped to JavaScript Number.
  564. ## API
  565. I provide all necessary methods for arithmetic operations, comparison, conversion and so on with numbers.
  566. My instances can also be used to evaluate a block a fixed number of times:
  567. 5 timesRepeat: [ Transcript show: 'This will be printed 5 times'; cr ].
  568. 1 to: 5 do: [ :aNumber| Transcript show: aNumber asString; cr ].
  569. 1 to: 10 by: 2 do: [ :aNumber| Transcript show: aNumber asString; cr ].!
  570. !Number methodsFor: 'arithmetic'!
  571. * aNumber
  572. "Inlined in the Compiler"
  573. <return self * aNumber>
  574. !
  575. + aNumber
  576. "Inlined in the Compiler"
  577. <return self + aNumber>
  578. !
  579. - aNumber
  580. "Inlined in the Compiler"
  581. <return self - aNumber>
  582. !
  583. / aNumber
  584. "Inlined in the Compiler"
  585. <return self / aNumber>
  586. !
  587. // aNumber
  588. ^ (self / aNumber) floor
  589. !
  590. \\ aNumber
  591. <return self % aNumber>
  592. !
  593. abs
  594. <return Math.abs(self);>
  595. !
  596. max: aNumber
  597. <return Math.max(self, aNumber);>
  598. !
  599. min: aNumber
  600. <return Math.min(self, aNumber);>
  601. !
  602. negated
  603. ^ 0 - self
  604. ! !
  605. !Number methodsFor: 'comparing'!
  606. < aNumber
  607. "Inlined in the Compiler"
  608. <return self < aNumber>
  609. !
  610. <= aNumber
  611. "Inlined in the Compiler"
  612. <return self <= aNumber>
  613. !
  614. == aNumber
  615. <return aNumber !!= null && Number(self) === (typeof aNumber === "number" ? aNumber : aNumber.valueOf())>
  616. !
  617. > aNumber
  618. "Inlined in the Compiler"
  619. <return self >> aNumber>
  620. !
  621. >= aNumber
  622. "Inlined in the Compiler"
  623. <return self >>= aNumber>
  624. ! !
  625. !Number methodsFor: 'converting'!
  626. & aNumber
  627. <return self & aNumber>
  628. !
  629. @ aNumber
  630. ^ Point x: self y: aNumber
  631. !
  632. asJSON
  633. ^ self
  634. !
  635. asJavascript
  636. ^ '(', self printString, ')'
  637. !
  638. asNumber
  639. ^ self
  640. !
  641. asPoint
  642. ^ Point x: self y: self
  643. !
  644. asString
  645. < return String(self) >
  646. !
  647. atRandom
  648. ^ (Random new next * self) truncated + 1
  649. !
  650. ceiling
  651. <return Math.ceil(self);>
  652. !
  653. floor
  654. <return Math.floor(self);>
  655. !
  656. rounded
  657. <return Math.round(self);>
  658. !
  659. to: aNumber
  660. | array first last count |
  661. first := self truncated.
  662. last := aNumber truncated + 1.
  663. count := 1.
  664. array := Array new.
  665. (last - first) timesRepeat: [
  666. array at: count put: first.
  667. count := count + 1.
  668. first := first + 1 ].
  669. ^ array
  670. !
  671. to: stop by: step
  672. | array value pos |
  673. value := self.
  674. array := Array new.
  675. pos := 1.
  676. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  677. step < 0
  678. ifTrue: [ [ value >= stop ] whileTrue: [
  679. array at: pos put: value.
  680. pos := pos + 1.
  681. value := value + step ]]
  682. ifFalse: [ [ value <= stop ] whileTrue: [
  683. array at: pos put: value.
  684. pos := pos + 1.
  685. value := value + step ]].
  686. ^ array
  687. !
  688. truncated
  689. <
  690. if(self >>= 0) {
  691. return Math.floor(self);
  692. } else {
  693. return Math.floor(self * (-1)) * (-1);
  694. };
  695. >
  696. !
  697. | aNumber
  698. <return self | aNumber>
  699. ! !
  700. !Number methodsFor: 'copying'!
  701. copy
  702. ^ self
  703. !
  704. deepCopy
  705. ^ self copy
  706. ! !
  707. !Number methodsFor: 'enumerating'!
  708. timesRepeat: aBlock
  709. | count |
  710. count := 1.
  711. [ count > self ] whileFalse: [
  712. aBlock value.
  713. count := count + 1 ]
  714. !
  715. to: stop by: step do: aBlock
  716. | value |
  717. value := self.
  718. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  719. step < 0
  720. ifTrue: [ [ value >= stop ] whileTrue: [
  721. aBlock value: value.
  722. value := value + step ]]
  723. ifFalse: [ [ value <= stop ] whileTrue: [
  724. aBlock value: value.
  725. value := value + step ]]
  726. !
  727. to: stop do: aBlock
  728. "Evaluate aBlock for each number from self to aNumber."
  729. | nextValue |
  730. nextValue := self.
  731. [ nextValue <= stop ]
  732. whileTrue:
  733. [ aBlock value: nextValue.
  734. nextValue := nextValue + 1 ]
  735. ! !
  736. !Number methodsFor: 'mathematical functions'!
  737. ** exponent
  738. ^ self raisedTo: exponent
  739. !
  740. arcCos
  741. <return Math.acos(self);>
  742. !
  743. arcSin
  744. <return Math.asin(self);>
  745. !
  746. arcTan
  747. <return Math.atan(self);>
  748. !
  749. cos
  750. <return Math.cos(self);>
  751. !
  752. ln
  753. <return Math.log(self);>
  754. !
  755. log
  756. <return Math.log(self) / Math.LN10;>
  757. !
  758. log: aNumber
  759. <return Math.log(self) / Math.log(aNumber);>
  760. !
  761. raisedTo: exponent
  762. <return Math.pow(self, exponent);>
  763. !
  764. sign
  765. self isZero
  766. ifTrue: [ ^ 0 ].
  767. self positive
  768. ifTrue: [ ^ 1 ]
  769. ifFalse: [ ^ -1 ].
  770. !
  771. sin
  772. <return Math.sin(self);>
  773. !
  774. sqrt
  775. <return Math.sqrt(self)>
  776. !
  777. squared
  778. ^ self * self
  779. !
  780. tan
  781. <return Math.tan(self);>
  782. ! !
  783. !Number methodsFor: 'printing'!
  784. printOn: aStream
  785. aStream nextPutAll: self asString
  786. !
  787. printShowingDecimalPlaces: placesDesired
  788. <return self.toFixed(placesDesired)>
  789. ! !
  790. !Number methodsFor: 'testing'!
  791. even
  792. ^ 0 = (self \\ 2)
  793. !
  794. isImmutable
  795. ^ true
  796. !
  797. isNumber
  798. ^ true
  799. !
  800. isZero
  801. ^ self = 0
  802. !
  803. negative
  804. "Answer whether the receiver is mathematically negative."
  805. ^ self < 0
  806. !
  807. odd
  808. ^ self even not
  809. !
  810. positive
  811. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  812. ^ self >= 0
  813. ! !
  814. !Number class methodsFor: 'accessing'!
  815. classTag
  816. "Returns a tag or general category for this class.
  817. Typically used to help tools do some reflection.
  818. Helios, for example, uses this to decide what icon the class should display."
  819. ^ 'magnitude'
  820. ! !
  821. !Number class methodsFor: 'instance creation'!
  822. e
  823. <return Math.E;>
  824. !
  825. pi
  826. <return Math.PI>
  827. ! !
  828. Object subclass: #Point
  829. instanceVariableNames: 'x y'
  830. package: 'Kernel-Objects'!
  831. !Point commentStamp!
  832. I represent an x-y pair of numbers usually designating a geometric coordinate.
  833. ## API
  834. Instances are traditionally created using the binary `#@` message to a number:
  835. 100@120
  836. Points can then be arithmetically manipulated:
  837. 100@100 + (10@10)
  838. ...or for example:
  839. (100@100) * 2
  840. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  841. 100@ -100 "but 100@-100 would not parse"!
  842. !Point methodsFor: 'accessing'!
  843. x
  844. ^ x
  845. !
  846. x: aNumber
  847. x := aNumber
  848. !
  849. y
  850. ^ y
  851. !
  852. y: aNumber
  853. y := aNumber
  854. ! !
  855. !Point methodsFor: 'arithmetic'!
  856. * aPoint
  857. ^ Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  858. !
  859. + aPoint
  860. ^ Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  861. !
  862. - aPoint
  863. ^ Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  864. !
  865. / aPoint
  866. ^ Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  867. ! !
  868. !Point methodsFor: 'comparing'!
  869. < aPoint
  870. ^ self x < aPoint x and: [
  871. self y < aPoint y ]
  872. !
  873. <= aPoint
  874. ^ self x <= aPoint x and: [
  875. self y <= aPoint y ]
  876. !
  877. = aPoint
  878. ^ aPoint class = self class and: [
  879. (aPoint x = self x) & (aPoint y = self y) ]
  880. !
  881. > aPoint
  882. ^ self x > aPoint x and: [
  883. self y > aPoint y ]
  884. !
  885. >= aPoint
  886. ^ self x >= aPoint x and: [
  887. self y >= aPoint y ]
  888. ! !
  889. !Point methodsFor: 'converting'!
  890. asPoint
  891. ^ self
  892. ! !
  893. !Point methodsFor: 'printing'!
  894. printOn: aStream
  895. "Print receiver in classic x@y notation."
  896. x printOn: aStream.
  897. aStream nextPutAll: '@'.
  898. (y notNil and: [ y negative ]) ifTrue: [
  899. "Avoid ambiguous @- construct"
  900. aStream space ].
  901. y printOn: aStream
  902. ! !
  903. !Point methodsFor: 'transforming'!
  904. dist: aPoint
  905. "Answer the distance between aPoint and the receiver."
  906. | dx dy |
  907. dx := aPoint x - x.
  908. dy := aPoint y - y.
  909. ^ (dx * dx + (dy * dy)) sqrt
  910. !
  911. translateBy: delta
  912. "Answer a Point translated by delta (an instance of Point)."
  913. ^ (delta x + x) @ (delta y + y)
  914. ! !
  915. !Point class methodsFor: 'accessing'!
  916. classTag
  917. "Returns a tag or general category for this class.
  918. Typically used to help tools do some reflection.
  919. Helios, for example, uses this to decide what icon the class should display."
  920. ^ 'magnitude'
  921. ! !
  922. !Point class methodsFor: 'instance creation'!
  923. x: aNumber y: anotherNumber
  924. ^ self new
  925. x: aNumber;
  926. y: anotherNumber;
  927. yourself
  928. ! !
  929. Object subclass: #Random
  930. instanceVariableNames: ''
  931. package: 'Kernel-Objects'!
  932. !Random commentStamp!
  933. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  934. ## API
  935. The typical use case it to use the `#next` method like the following:
  936. Random new next
  937. This will return a float x where x < 1 and x > 0. If you want a random integer from 1 to 10 you can use `#atRandom`
  938. 10 atRandom
  939. A random number in a specific interval can be obtained with the following:
  940. (3 to: 7) atRandom
  941. Be aware that `#to:` does not create an Interval as in other Smalltalk implementations but in fact an `Array` of numbers, so it's better to use:
  942. 5 atRandom + 2
  943. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  944. #('a' 'b' 'c') atRandom
  945. As well as letter from a `String`:
  946. 'abc' atRandom
  947. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  948. !Random methodsFor: 'accessing'!
  949. next
  950. <return Math.random()>
  951. !
  952. next: anInteger
  953. ^ (1 to: anInteger) collect: [ :each | self next ]
  954. ! !
  955. Object subclass: #UndefinedObject
  956. instanceVariableNames: ''
  957. package: 'Kernel-Objects'!
  958. !UndefinedObject commentStamp!
  959. I describe the behavior of my sole instance, `nil`. `nil` represents a prior value for variables that have not been initialized, or for results which are meaningless.
  960. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  961. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  962. !UndefinedObject methodsFor: 'class creation'!
  963. subclass: aString instanceVariableNames: anotherString
  964. "Kept for file-in compatibility."
  965. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  966. !
  967. subclass: aString instanceVariableNames: aString2 category: aString3
  968. "Kept for file-in compatibility."
  969. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  970. !
  971. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  972. "Kept for file-in compatibility. ignores class variables and pools."
  973. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  974. !
  975. subclass: aString instanceVariableNames: aString2 package: aString3
  976. ^ ClassBuilder new
  977. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  978. ! !
  979. !UndefinedObject methodsFor: 'converting'!
  980. asJSON
  981. ^ null
  982. ! !
  983. !UndefinedObject methodsFor: 'copying'!
  984. deepCopy
  985. ^ self
  986. !
  987. shallowCopy
  988. ^ self
  989. ! !
  990. !UndefinedObject methodsFor: 'printing'!
  991. printOn: aStream
  992. aStream nextPutAll: 'nil'
  993. ! !
  994. !UndefinedObject methodsFor: 'testing'!
  995. ifNil: aBlock
  996. "inlined in the Compiler"
  997. ^ self ifNil: aBlock ifNotNil: []
  998. !
  999. ifNil: aBlock ifNotNil: anotherBlock
  1000. "inlined in the Compiler"
  1001. ^ aBlock value
  1002. !
  1003. ifNotNil: aBlock
  1004. "inlined in the Compiler"
  1005. ^ self
  1006. !
  1007. ifNotNil: aBlock ifNil: anotherBlock
  1008. "inlined in the Compiler"
  1009. ^ anotherBlock value
  1010. !
  1011. isImmutable
  1012. ^ true
  1013. !
  1014. isNil
  1015. ^ true
  1016. !
  1017. notNil
  1018. ^ false
  1019. ! !
  1020. !UndefinedObject class methodsFor: 'instance creation'!
  1021. new
  1022. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1023. ! !