Kernel-Objects.st 22 KB

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