Kernel-Objects.st 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. Smalltalk createPackage: 'Kernel-Objects'!
  2. nil subclass: #ProtoObject
  3. slots: {}
  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. <inlineJS: 'return $self.a$cls'>
  11. !
  12. identityHash
  13. <inlineJS: '
  14. self._deprecatedAPI();
  15. var hash=self.identityHash;
  16. if (hash) return hash;
  17. hash=$core.nextId();
  18. Object.defineProperty(self, ''identityHash'', {value:hash});
  19. return hash;
  20. '>
  21. !
  22. instVarNamed: aString
  23. <inlineJS: 'return $self[aString]'>
  24. !
  25. instVarNamed: aString put: anObject
  26. <inlineJS: '$self[aString] = anObject'>
  27. !
  28. yourself
  29. ^ self
  30. ! !
  31. !ProtoObject methodsFor: 'comparing'!
  32. = anObject
  33. ^ self == anObject
  34. !
  35. == anObject
  36. <inlineJS: 'return self === anObject'>
  37. !
  38. ~= anObject
  39. ^ (self = anObject) = false
  40. !
  41. ~~ anObject
  42. ^ (self == anObject) = false
  43. ! !
  44. !ProtoObject methodsFor: 'converting'!
  45. asString
  46. ^ self printString
  47. ! !
  48. !ProtoObject methodsFor: 'error handling'!
  49. doesNotUnderstand: aMessage
  50. MessageNotUnderstood new
  51. receiver: self;
  52. message: aMessage;
  53. signal
  54. ! !
  55. !ProtoObject methodsFor: 'evaluating'!
  56. evaluate: aString on: anEvaluator
  57. ^ anEvaluator evaluate: aString receiver: self
  58. ! !
  59. !ProtoObject methodsFor: 'initialization'!
  60. initialize
  61. ! !
  62. !ProtoObject methodsFor: 'inspecting'!
  63. inspect
  64. Inspector inspect: self
  65. !
  66. inspectOn: anInspector
  67. ! !
  68. !ProtoObject methodsFor: 'message handling'!
  69. perform: aString
  70. ^ self perform: aString withArguments: #()
  71. !
  72. perform: aString withArguments: aCollection
  73. <inlineJS: 'return $core.send2(self, aString, aCollection)'>
  74. ! !
  75. !ProtoObject methodsFor: 'printing'!
  76. printOn: aStream
  77. aStream nextPutAll: (self class name first isVowel
  78. ifTrue: [ 'an ' ]
  79. ifFalse: [ 'a ' ]).
  80. aStream nextPutAll: self class name
  81. !
  82. printString
  83. ^ String streamContents: [ :str |
  84. self printOn: str ]
  85. ! !
  86. !ProtoObject methodsFor: 'testing'!
  87. ifNil: aBlock
  88. "inlined in the Compiler"
  89. ^ self
  90. !
  91. ifNil: aBlock ifNotNil: anotherBlock
  92. "inlined in the Compiler"
  93. ^ anotherBlock value: self
  94. !
  95. ifNotNil: aBlock
  96. "inlined in the Compiler"
  97. ^ aBlock value: self
  98. !
  99. ifNotNil: aBlock ifNil: anotherBlock
  100. "inlined in the Compiler"
  101. ^ aBlock value: self
  102. !
  103. isKindOf: aClass
  104. ^ (self isMemberOf: aClass)
  105. ifTrue: [ true ]
  106. ifFalse: [ self class inheritsFrom: aClass ]
  107. !
  108. isNil
  109. ^ false
  110. !
  111. notNil
  112. ^ true
  113. ! !
  114. !ProtoObject class methodsFor: 'initialization'!
  115. initialize
  116. ! !
  117. ProtoObject subclass: #Object
  118. slots: {}
  119. package: 'Kernel-Objects'!
  120. !Object commentStamp!
  121. **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.
  122. I provide default behavior common to all normal objects (some of it inherited from `ProtoObject`), such as:
  123. - accessing
  124. - copying
  125. - comparison
  126. - error handling
  127. - message sending
  128. - reflection
  129. Also utility messages that all objects should respond to are defined here.
  130. I have no instance variable.
  131. ##Access
  132. Instance variables can be accessed with `#instVarAt:` and `#instVarAt:put:`. `#instanceVariableNames` answers a collection of all instance variable names.
  133. Accessing JavaScript properties of an object is done through `#basicAt:`, `#basicAt:put:` and `basicDelete:`.
  134. ##Copying
  135. 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.
  136. 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.
  137. ##Comparison
  138. I understand equality `#=` and identity `#==` comparison.
  139. ##Error handling
  140. - `#halt` is the typical message to use for inserting breakpoints during debugging.
  141. - `#error:` throws a generic error exception
  142. - `#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.
  143. Overriding this message can be useful to implement proxies for example.!
  144. !Object methodsFor: 'accessing'!
  145. basicAt: aString
  146. <inlineJS: 'return self[aString]'>
  147. !
  148. basicAt: aString put: anObject
  149. <inlineJS: 'return self[aString] = anObject'>
  150. !
  151. basicDelete: aString
  152. <inlineJS: 'delete self[aString]; return aString'>
  153. !
  154. divideBySelfToNumber: aNumber
  155. self error: 'I am not a number.'
  156. !
  157. divisionRemainderBySelfToNumber: aNumber
  158. self error: 'I am not a number.'
  159. !
  160. minusSelfToNumber: aNumber
  161. self error: 'I am not a number.'
  162. !
  163. plusSelfToNumber: aNumber
  164. self error: 'I am not a number.'
  165. !
  166. size
  167. self error: 'Object not indexable'
  168. !
  169. timesSelfToNumber: aNumber
  170. self error: 'I am not a number.'
  171. ! !
  172. !Object methodsFor: 'browsing'!
  173. browse
  174. Finder findClass: self class
  175. ! !
  176. !Object methodsFor: 'comparing'!
  177. isNumberEqualToSelf: aNumber
  178. ^ false
  179. !
  180. isNumberGreaterThanOrEqualToSelf: aNumber
  181. ^ false
  182. !
  183. isNumberGreaterThanSelf: aNumber
  184. ^ false
  185. !
  186. isNumberLessThanOrEqualToSelf: aNumber
  187. ^ false
  188. !
  189. isNumberLessThanSelf: aNumber
  190. ^ false
  191. !
  192. isStringEqualToSelf: aString
  193. ^ false
  194. !
  195. isStringGreaterThanOrEqualToSelf: aString
  196. ^ false
  197. !
  198. isStringGreaterThanSelf: aString
  199. ^ false
  200. !
  201. isStringLessThanOrEqualToSelf: aString
  202. ^ false
  203. !
  204. isStringLessThanSelf: aString
  205. ^ false
  206. ! !
  207. !Object methodsFor: 'converting'!
  208. -> anObject
  209. ^ Association key: self value: anObject
  210. !
  211. andSelfToNumber: aNumber
  212. self error: 'I am not a number.'
  213. !
  214. asJSONString
  215. ^ JSON stringify: self asJavaScriptObject
  216. !
  217. asJavaScriptObject
  218. | variables |
  219. variables := HashedCollection new.
  220. self class allInstanceVariableNames do: [ :each |
  221. variables at: each put: (self instVarNamed: each) asJavaScriptObject ].
  222. ^ variables
  223. !
  224. asJavaScriptSource
  225. ^ self asString
  226. !
  227. orSelfToNumber: aNumber
  228. self error: 'I am not a number.'
  229. !
  230. xorSelfToNumber: aNumber
  231. self error: 'I am not a number.'
  232. ! !
  233. !Object methodsFor: 'copying'!
  234. appendToString: aString
  235. self error: 'Cannot add self to a string.'
  236. !
  237. copy
  238. ^ self shallowCopy postCopy
  239. !
  240. deepCopy
  241. <inlineJS: '
  242. var copy = self.a$cls._new();
  243. Object.keys(self).forEach(function (i) {
  244. copy[i] = $recv(self[i])._deepCopy();
  245. });
  246. return copy;
  247. '>
  248. !
  249. postCopy
  250. !
  251. shallowCopy
  252. <inlineJS: '
  253. var copy = self.a$cls._new();
  254. Object.keys(self).forEach(function(i) {
  255. copy[i] = self[i];
  256. });
  257. return copy;
  258. '>
  259. ! !
  260. !Object methodsFor: 'error handling'!
  261. deprecatedAPI
  262. "Just a simple way to deprecate methods.
  263. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  264. but it could in the future."
  265. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'.
  266. !
  267. deprecatedAPI: aString
  268. "Just a simple way to deprecate methods.
  269. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  270. but it could in the future."
  271. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'.
  272. console warn: aString
  273. !
  274. error: aString
  275. Error signal: aString
  276. !
  277. halt
  278. Halt signal
  279. !
  280. shouldNotImplement
  281. self error: 'This method should not be implemented in ', self class name
  282. !
  283. subclassResponsibility
  284. self error: 'This method is a responsibility of a subclass'
  285. ! !
  286. !Object methodsFor: 'evaluating'!
  287. in: aValuable
  288. ^ aValuable value: self
  289. !
  290. value
  291. <inlineJS: 'return self.valueOf()'>
  292. ! !
  293. !Object methodsFor: 'message handling'!
  294. basicPerform: aString
  295. ^ self basicPerform: aString withArguments: #()
  296. !
  297. basicPerform: aString withArguments: aCollection
  298. <inlineJS: 'return self[aString].apply(self, aCollection);'>
  299. ! !
  300. !Object methodsFor: 'streaming'!
  301. putOn: aStream
  302. aStream nextPut: self
  303. ! !
  304. !Object methodsFor: 'testing'!
  305. isImmutable
  306. ^ false
  307. !
  308. isMemberOf: aClass
  309. ^ self class = aClass
  310. !
  311. isParseFailure
  312. ^ false
  313. !
  314. respondsTo: aSelector
  315. ^ self class canUnderstand: aSelector
  316. ! !
  317. !Object class methodsFor: 'helios'!
  318. accessorProtocolWith: aGenerator
  319. aGenerator accessorProtocolForObject
  320. !
  321. accessorsSourceCodesWith: aGenerator
  322. aGenerator accessorsForObject
  323. !
  324. initializeProtocolWith: aGenerator
  325. aGenerator initializeProtocolForObject
  326. !
  327. initializeSourceCodesWith: aGenerator
  328. aGenerator initializeForObject
  329. ! !
  330. !Object class methodsFor: 'initialization'!
  331. initialize
  332. "no op"
  333. ! !
  334. Object subclass: #Boolean
  335. slots: {}
  336. package: 'Kernel-Objects'!
  337. !Boolean commentStamp!
  338. I define the protocol for logic testing operations and conditional control structures for the logical values (see the `controlling` protocol).
  339. I have two instances, `true` and `false`.
  340. I am directly mapped to JavaScript Boolean. The `true` and `false` objects are the JavaScript boolean objects.
  341. ## Usage Example:
  342. aBoolean not ifTrue: [ ... ] ifFalse: [ ... ]!
  343. !Boolean methodsFor: 'comparing'!
  344. == aBoolean
  345. <inlineJS: '
  346. if (typeof aBoolean === "boolean") return (self == true) === aBoolean;
  347. else if (aBoolean !!= null && typeof aBoolean === "object") return (self == true) === aBoolean.valueOf();
  348. else return false;
  349. '>
  350. ! !
  351. !Boolean methodsFor: 'controlling'!
  352. & aBoolean
  353. <inlineJS: '
  354. if(self == true) {
  355. return aBoolean;
  356. } else {
  357. return false;
  358. }
  359. '>
  360. !
  361. and: aBlock
  362. ^ self
  363. ifTrue: "aBlock" [ aBlock value ]
  364. ifFalse: [ false ]
  365. !
  366. ifFalse: aBlock
  367. "inlined in the Compiler"
  368. ^ self ifTrue: [] ifFalse: aBlock
  369. !
  370. ifFalse: aBlock ifTrue: anotherBlock
  371. "inlined in the Compiler"
  372. ^ self ifTrue: anotherBlock ifFalse: aBlock
  373. !
  374. ifTrue: aBlock
  375. "inlined in the Compiler"
  376. ^ self ifTrue: aBlock ifFalse: []
  377. !
  378. ifTrue: aBlock ifFalse: anotherBlock
  379. "inlined in the Compiler"
  380. <inlineJS: '
  381. if(self == true) {
  382. return aBlock._value();
  383. } else {
  384. return anotherBlock._value();
  385. }
  386. '>
  387. !
  388. not
  389. ^ self = false
  390. !
  391. or: aBlock
  392. ^ self
  393. ifTrue: [ true ]
  394. ifFalse: "aBlock" [ aBlock value ]
  395. !
  396. | aBoolean
  397. <inlineJS: '
  398. if(self == true) {
  399. return true;
  400. } else {
  401. return aBoolean;
  402. }
  403. '>
  404. ! !
  405. !Boolean methodsFor: 'converting'!
  406. asBit
  407. ^ self ifTrue: [ 1 ] ifFalse: [ 0 ]
  408. !
  409. asJavaScriptObject
  410. ^ self
  411. !
  412. asString
  413. <inlineJS: 'return self.toString()'>
  414. ! !
  415. !Boolean methodsFor: 'copying'!
  416. deepCopy
  417. ^ self
  418. !
  419. shallowCopy
  420. ^ self
  421. ! !
  422. !Boolean methodsFor: 'printing'!
  423. printOn: aStream
  424. aStream nextPutAll: self asString
  425. ! !
  426. !Boolean methodsFor: 'testing'!
  427. isBoolean
  428. ^ true
  429. !
  430. isImmutable
  431. ^ true
  432. ! !
  433. Object subclass: #Date
  434. slots: {}
  435. package: 'Kernel-Objects'!
  436. !Date commentStamp!
  437. I am used to work with both dates and times. Therefore `Date today` and `Date now` are both valid in
  438. Amber and answer the same date object.
  439. Date directly maps to the `Date()` JavaScript constructor, and Amber date objects are JavaScript date objects.
  440. ## API
  441. The class-side `instance creation` protocol contains some convenience methods for creating date/time objects such as `#fromSeconds:`.
  442. Arithmetic and comparison is supported (see the `comparing` and `arithmetic` protocols).
  443. The `converting` protocol provides convenience methods for various convertions (to numbers, strings, etc.).!
  444. !Date methodsFor: 'accessing'!
  445. day
  446. ^ self dayOfWeek
  447. !
  448. day: aNumber
  449. self dayOfWeek: aNumber
  450. !
  451. dayOfMonth
  452. <inlineJS: 'return self.getDate()'>
  453. !
  454. dayOfMonth: aNumber
  455. <inlineJS: 'self.setDate(aNumber)'>
  456. !
  457. dayOfWeek
  458. <inlineJS: 'return self.getDay() + 1'>
  459. !
  460. dayOfWeek: aNumber
  461. <inlineJS: 'return self.setDay(aNumber - 1)'>
  462. !
  463. hours
  464. <inlineJS: 'return self.getHours()'>
  465. !
  466. hours: aNumber
  467. <inlineJS: 'self.setHours(aNumber)'>
  468. !
  469. milliseconds
  470. <inlineJS: 'return self.getMilliseconds()'>
  471. !
  472. milliseconds: aNumber
  473. <inlineJS: 'self.setMilliseconds(aNumber)'>
  474. !
  475. minutes
  476. <inlineJS: 'return self.getMinutes()'>
  477. !
  478. minutes: aNumber
  479. <inlineJS: 'self.setMinutes(aNumber)'>
  480. !
  481. month
  482. <inlineJS: 'return self.getMonth() + 1'>
  483. !
  484. month: aNumber
  485. <inlineJS: 'self.setMonth(aNumber - 1)'>
  486. !
  487. seconds
  488. <inlineJS: 'return self.getSeconds()'>
  489. !
  490. seconds: aNumber
  491. <inlineJS: 'self.setSeconds(aNumber)'>
  492. !
  493. time
  494. <inlineJS: 'return self.getTime()'>
  495. !
  496. time: aNumber
  497. <inlineJS: 'self.setTime(aNumber)'>
  498. !
  499. year
  500. <inlineJS: 'return self.getFullYear()'>
  501. !
  502. year: aNumber
  503. <inlineJS: 'self.setFullYear(aNumber)'>
  504. ! !
  505. !Date methodsFor: 'arithmetic'!
  506. + aNumber
  507. <inlineJS: 'return new Date($self.getTime() + aNumber)'>
  508. !
  509. - aDate
  510. <inlineJS: 'return self - aDate'>
  511. ! !
  512. !Date methodsFor: 'comparing'!
  513. < aDate
  514. <inlineJS: 'return self < aDate'>
  515. !
  516. <= aDate
  517. <inlineJS: 'return self <= aDate'>
  518. !
  519. = aDate
  520. ^ (aDate class == self class) and: [ self asMilliseconds == aDate asMilliseconds ]
  521. !
  522. > aDate
  523. <inlineJS: 'return self > aDate'>
  524. !
  525. >= aDate
  526. <inlineJS: 'return self >= aDate'>
  527. ! !
  528. !Date methodsFor: 'converting'!
  529. asDateString
  530. <inlineJS: 'return self.toDateString()'>
  531. !
  532. asLocaleString
  533. <inlineJS: 'return self.toLocaleString()'>
  534. !
  535. asMilliseconds
  536. ^ self time
  537. !
  538. asNumber
  539. ^ self asMilliseconds
  540. !
  541. asString
  542. <inlineJS: 'return self.toString()'>
  543. !
  544. asTimeString
  545. <inlineJS: 'return self.toTimeString()'>
  546. ! !
  547. !Date methodsFor: 'printing'!
  548. printOn: aStream
  549. aStream nextPutAll: self asString
  550. ! !
  551. !Date class methodsFor: 'accessing'!
  552. classTag
  553. "Returns a tag or general category for this class.
  554. Typically used to help tools do some reflection.
  555. Helios, for example, uses this to decide what icon the class should display."
  556. ^ 'magnitude'
  557. ! !
  558. !Date class methodsFor: 'instance creation'!
  559. fromMilliseconds: aNumber
  560. ^ self new: aNumber
  561. !
  562. fromSeconds: aNumber
  563. ^ self fromMilliseconds: aNumber * 1000
  564. !
  565. fromString: aString
  566. "Example: Date fromString('2011/04/15 00:00:00')"
  567. ^ self new: aString
  568. !
  569. millisecondsToRun: aBlock
  570. | t |
  571. t := Date now.
  572. aBlock value.
  573. ^ Date now - t
  574. !
  575. new: anObject
  576. <inlineJS: 'return new Date(anObject)'>
  577. !
  578. now
  579. ^ self today
  580. !
  581. today
  582. ^ self new
  583. ! !
  584. Object subclass: #Number
  585. slots: {}
  586. package: 'Kernel-Objects'!
  587. !Number commentStamp!
  588. I am the Amber representation for all numbers.
  589. I am directly mapped to JavaScript Number.
  590. ## API
  591. I provide all necessary methods for arithmetic operations, comparison, conversion and so on with numbers.
  592. My instances can also be used to evaluate a block a fixed number of times:
  593. 5 timesRepeat: [ Transcript show: 'This will be printed 5 times'; cr ].
  594. 1 to: 5 do: [ :aNumber| Transcript show: aNumber asString; cr ].
  595. 1 to: 10 by: 2 do: [ :aNumber| Transcript show: aNumber asString; cr ].!
  596. !Number methodsFor: 'arithmetic'!
  597. * aNumber
  598. <inlineJS: 'return typeof aNumber === "number" ?
  599. self * aNumber :
  600. $recv(aNumber)._timesSelfToNumber_(self)'>
  601. !
  602. + aNumber
  603. <inlineJS: 'return typeof aNumber === "number" ?
  604. self + aNumber :
  605. $recv(aNumber)._plusSelfToNumber_(self)'>
  606. !
  607. - aNumber
  608. <inlineJS: 'return typeof aNumber === "number" ?
  609. self - aNumber :
  610. $recv(aNumber)._minusSelfToNumber_(self)'>
  611. !
  612. / aNumber
  613. <inlineJS: 'return typeof aNumber === "number" ?
  614. self / aNumber :
  615. $recv(aNumber)._divideBySelfToNumber_(self)'>
  616. !
  617. // aNumber
  618. ^ (self / aNumber) floor
  619. !
  620. \\ aNumber
  621. <inlineJS: 'return typeof aNumber === "number" ?
  622. self % aNumber :
  623. $recv(aNumber)._divisionRemainderBySelfToNumber_(self)'>
  624. !
  625. abs
  626. <inlineJS: 'return Math.abs(self);'>
  627. !
  628. divideBySelfToNumber: aNumber
  629. <inlineJS: 'return aNumber / self'>
  630. !
  631. divisionRemainderBySelfToNumber: aNumber
  632. <inlineJS: 'return aNumber % self'>
  633. !
  634. max: aNumber
  635. <inlineJS: 'return Math.max(self, aNumber);'>
  636. !
  637. min: aNumber
  638. <inlineJS: 'return Math.min(self, aNumber);'>
  639. !
  640. min: aMin max: aMax
  641. ^ (self min: aMin) max: aMax
  642. !
  643. minusSelfToNumber: aNumber
  644. <inlineJS: 'return aNumber - self'>
  645. !
  646. negated
  647. ^ 0 - self
  648. !
  649. plusSelfToNumber: aNumber
  650. <inlineJS: 'return aNumber + self'>
  651. !
  652. timesSelfToNumber: aNumber
  653. <inlineJS: 'return aNumber * self'>
  654. ! !
  655. !Number methodsFor: 'comparing'!
  656. < aNumber
  657. <inlineJS: 'return typeof aNumber === "number" ?
  658. Number(self) < aNumber :
  659. $recv(aNumber)._isNumberLessThanSelf_(self)'>
  660. !
  661. <= aNumber
  662. <inlineJS: 'return typeof aNumber === "number" ?
  663. Number(self) <= aNumber :
  664. $recv(aNumber)._isNumberLessThanOrEqualToSelf_(self)'>
  665. !
  666. == aNumber
  667. <inlineJS: 'return typeof aNumber === "number" ?
  668. Number(self) === aNumber :
  669. $recv(aNumber)._isNumberEqualToSelf_(self)'>
  670. !
  671. > aNumber
  672. <inlineJS: 'return typeof aNumber === "number" ?
  673. Number(self) > aNumber :
  674. $recv(aNumber)._isNumberGreaterThanSelf_(self)'>
  675. !
  676. >= aNumber
  677. <inlineJS: 'return typeof aNumber === "number" ?
  678. Number(self) >= aNumber :
  679. $recv(aNumber)._isNumberGreaterThanOrEqualToSelf_(self)'>
  680. !
  681. isNumberEqualToSelf: aNumber
  682. <inlineJS: 'return aNumber === Number(self)'>
  683. !
  684. isNumberGreaterThanOrEqualToSelf: aNumber
  685. <inlineJS: 'return aNumber >= self'>
  686. !
  687. isNumberGreaterThanSelf: aNumber
  688. <inlineJS: 'return aNumber > self'>
  689. !
  690. isNumberLessThanOrEqualToSelf: aNumber
  691. <inlineJS: 'return aNumber <= self'>
  692. !
  693. isNumberLessThanSelf: aNumber
  694. <inlineJS: 'return aNumber < self'>
  695. ! !
  696. !Number methodsFor: 'converting'!
  697. & aNumber
  698. ^ self bitAnd: aNumber
  699. !
  700. @ aNumber
  701. ^ Point x: self y: aNumber
  702. !
  703. andSelfToNumber: aNumber
  704. <inlineJS: 'return aNumber & self'>
  705. !
  706. asJavaScriptObject
  707. ^ self
  708. !
  709. asJavaScriptSource
  710. ^ '(', self printString, ')'
  711. !
  712. asNumber
  713. ^ self
  714. !
  715. asPoint
  716. ^ Point x: self y: self
  717. !
  718. asString
  719. <inlineJS: 'return String(self)'>
  720. !
  721. atRandom
  722. ^ (Random new next * self) truncated + 1
  723. !
  724. bitAnd: aNumber
  725. <inlineJS: 'return typeof aNumber === "number" ?
  726. self & aNumber :
  727. $recv(aNumber)._andSelfToNumber_(self)'>
  728. !
  729. bitNot
  730. <inlineJS: 'return ~self'>
  731. !
  732. bitOr: aNumber
  733. <inlineJS: 'return typeof aNumber === "number" ?
  734. self | aNumber :
  735. $recv(aNumber)._orSelfToNumber_(self)'>
  736. !
  737. bitXor: aNumber
  738. <inlineJS: 'return typeof aNumber === "number" ?
  739. self ^ aNumber :
  740. $recv(aNumber)._xorSelfToNumber_(self)'>
  741. !
  742. ceiling
  743. <inlineJS: 'return Math.ceil(self);'>
  744. !
  745. degreesToRadians
  746. ^ self * Number radiansPerDegree
  747. !
  748. floor
  749. <inlineJS: 'return Math.floor(self);'>
  750. !
  751. orSelfToNumber: aNumber
  752. <inlineJS: 'return aNumber | self'>
  753. !
  754. printStringBase: aBase
  755. <inlineJS: 'return self.toString(aBase)'>
  756. !
  757. radiansToDegrees
  758. ^ self / Number radiansPerDegree
  759. !
  760. rounded
  761. <inlineJS: 'return Math.round(self);'>
  762. !
  763. to: aNumber
  764. | array first last count |
  765. first := self truncated.
  766. last := aNumber truncated + 1.
  767. count := 1.
  768. array := Array new.
  769. (last - first) timesRepeat: [
  770. array at: count put: first.
  771. count := count + 1.
  772. first := first + 1 ].
  773. ^ array
  774. !
  775. to: stop by: step
  776. | array value pos |
  777. value := self.
  778. array := Array new.
  779. pos := 1.
  780. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  781. step < 0
  782. ifTrue: [ [ value >= stop ] whileTrue: [
  783. array at: pos put: value.
  784. pos := pos + 1.
  785. value := value + step ]]
  786. ifFalse: [ [ value <= stop ] whileTrue: [
  787. array at: pos put: value.
  788. pos := pos + 1.
  789. value := value + step ]].
  790. ^ array
  791. !
  792. truncated
  793. <inlineJS: '
  794. if(self >= 0) {
  795. return Math.floor(self);
  796. } else {
  797. return Math.floor(self * (-1)) * (-1);
  798. };
  799. '>
  800. !
  801. xorSelfToNumber: aNumber
  802. <inlineJS: 'return aNumber ^ self'>
  803. !
  804. | aNumber
  805. ^ self bitOr: aNumber
  806. ! !
  807. !Number methodsFor: 'copying'!
  808. copy
  809. ^ self
  810. !
  811. deepCopy
  812. ^ self copy
  813. ! !
  814. !Number methodsFor: 'enumerating'!
  815. timesRepeat: aBlock
  816. | count |
  817. count := 1.
  818. [ count > self ] whileFalse: [
  819. aBlock value.
  820. count := count + 1 ]
  821. !
  822. to: stop by: step do: aBlock
  823. | value |
  824. value := self.
  825. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  826. step < 0
  827. ifTrue: [ [ value >= stop ] whileTrue: [
  828. aBlock value: value.
  829. value := value + step ]]
  830. ifFalse: [ [ value <= stop ] whileTrue: [
  831. aBlock value: value.
  832. value := value + step ]]
  833. !
  834. to: stop do: aBlock
  835. "Evaluate aBlock for each number from self to aNumber."
  836. | nextValue |
  837. nextValue := self.
  838. [ nextValue <= stop ]
  839. whileTrue:
  840. [ aBlock value: nextValue.
  841. nextValue := nextValue + 1 ]
  842. ! !
  843. !Number methodsFor: 'mathematical functions'!
  844. ** exponent
  845. ^ self raisedTo: exponent
  846. !
  847. arcCos
  848. <inlineJS: 'return Math.acos(self);'>
  849. !
  850. arcSin
  851. <inlineJS: 'return Math.asin(self);'>
  852. !
  853. arcTan
  854. <inlineJS: 'return Math.atan(self);'>
  855. !
  856. arcTan: aNumber
  857. <inlineJS: 'return Math.atan2(self, aNumber);'>
  858. !
  859. cos
  860. <inlineJS: 'return Math.cos(self);'>
  861. !
  862. ln
  863. <inlineJS: 'return Math.log(self);'>
  864. !
  865. ln1p
  866. <inlineJS: 'return Math.log1p(self);'>
  867. !
  868. log
  869. <inlineJS: 'return Math.log(self) / Math.LN10;'>
  870. !
  871. log: aNumber
  872. <inlineJS: 'return Math.log(self) / Math.log(aNumber);'>
  873. !
  874. raisedTo: exponent
  875. <inlineJS: 'return Math.pow(self, exponent);'>
  876. !
  877. sign
  878. self isZero
  879. ifTrue: [ ^ 0 ].
  880. self positive
  881. ifTrue: [ ^ 1 ]
  882. ifFalse: [ ^ -1 ].
  883. !
  884. sin
  885. <inlineJS: 'return Math.sin(self);'>
  886. !
  887. sqrt
  888. <inlineJS: 'return Math.sqrt(self)'>
  889. !
  890. squared
  891. ^ self * self
  892. !
  893. tan
  894. <inlineJS: 'return Math.tan(self);'>
  895. ! !
  896. !Number methodsFor: 'printing'!
  897. printOn: aStream
  898. aStream nextPutAll: self asString
  899. !
  900. printShowingDecimalPlaces: placesDesired
  901. <inlineJS: 'return self.toFixed(placesDesired)'>
  902. ! !
  903. !Number methodsFor: 'testing'!
  904. between: min and: max
  905. ^ self >= min and: [ self <= max ]
  906. !
  907. even
  908. ^ 0 = (self \\ 2)
  909. !
  910. isFinite
  911. "Answer whether the receiver is finite"
  912. <inlineJS: 'return Number.isFinite(self)'>
  913. !
  914. isImmutable
  915. ^ true
  916. !
  917. isNaN
  918. "Answer whether the receiver is IEEE-754 not-a-number"
  919. <inlineJS: 'return Number.isNaN(self)'>
  920. !
  921. isNumber
  922. ^ true
  923. !
  924. isZero
  925. ^ self = 0
  926. !
  927. negative
  928. "Answer whether the receiver is mathematically negative."
  929. ^ self < 0
  930. !
  931. odd
  932. ^ self even not
  933. !
  934. positive
  935. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  936. ^ self >= 0
  937. ! !
  938. !Number class methodsFor: 'accessing'!
  939. classTag
  940. "Returns a tag or general category for this class.
  941. Typically used to help tools do some reflection.
  942. Helios, for example, uses this to decide what icon the class should display."
  943. ^ 'magnitude'
  944. ! !
  945. !Number class methodsFor: 'instance creation'!
  946. e
  947. <inlineJS: 'return Math.E;'>
  948. !
  949. negativeInfinity
  950. <inlineJS: 'return Number.NEGATIVE_INFINITY'>
  951. !
  952. pi
  953. <inlineJS: 'return Math.PI'>
  954. !
  955. positiveInfinity
  956. <inlineJS: 'return Number.POSITIVE_INFINITY'>
  957. !
  958. radiansPerDegree
  959. ^ (self pi) / 180
  960. ! !
  961. Object subclass: #Point
  962. slots: {#x. #y}
  963. package: 'Kernel-Objects'!
  964. !Point commentStamp!
  965. I represent an x-y pair of numbers usually designating a geometric coordinate.
  966. ## API
  967. Instances are traditionally created using the binary `#@` message to a number:
  968. 100@120
  969. Points can then be arithmetically manipulated:
  970. 100@100 + (10@10)
  971. ...or for example:
  972. (100@100) * 2
  973. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  974. 100@ -100 "but 100@-100 would not parse"!
  975. !Point methodsFor: 'accessing'!
  976. x
  977. ^ x
  978. !
  979. x: aNumber
  980. x := aNumber
  981. !
  982. y
  983. ^ y
  984. !
  985. y: aNumber
  986. y := aNumber
  987. ! !
  988. !Point methodsFor: 'arithmetic'!
  989. * aPoint
  990. ^ Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  991. !
  992. + aPoint
  993. ^ Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  994. !
  995. - aPoint
  996. ^ Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  997. !
  998. / aPoint
  999. ^ Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1000. ! !
  1001. !Point methodsFor: 'comparing'!
  1002. < aPoint
  1003. ^ self x < aPoint x and: [
  1004. self y < aPoint y ]
  1005. !
  1006. <= aPoint
  1007. ^ self x <= aPoint x and: [
  1008. self y <= aPoint y ]
  1009. !
  1010. = aPoint
  1011. ^ aPoint class = self class and: [
  1012. (aPoint x = self x) & (aPoint y = self y) ]
  1013. !
  1014. > aPoint
  1015. ^ self x > aPoint x and: [
  1016. self y > aPoint y ]
  1017. !
  1018. >= aPoint
  1019. ^ self x >= aPoint x and: [
  1020. self y >= aPoint y ]
  1021. ! !
  1022. !Point methodsFor: 'converting'!
  1023. asPoint
  1024. ^ self
  1025. ! !
  1026. !Point methodsFor: 'geometry'!
  1027. angle
  1028. ^ self y arcTan: self x
  1029. ! !
  1030. !Point methodsFor: 'point functions'!
  1031. dotProduct: aPoint
  1032. ^ (x * aPoint x) + (y * aPoint y)
  1033. !
  1034. normal
  1035. "Answer a Point representing the unit vector rotated 90 deg clockwise. For the zero point return -1@0."
  1036. | n d |
  1037. n := y negated @ x.
  1038. (d := (n x * n x + (n y * n y))) = 0
  1039. ifTrue: [ ^ -1 @0 ].
  1040. ^ n / d sqrt
  1041. !
  1042. normalized
  1043. | r |
  1044. r := self r.
  1045. r = 0
  1046. ifTrue: [ ^ Point x: 0 y: 0 ]
  1047. ifFalse: [ ^ Point x: x / r y: y / r ]
  1048. ! !
  1049. !Point methodsFor: 'polar coordinates'!
  1050. r
  1051. ^ ((x * x) + (y * y)) sqrt
  1052. ! !
  1053. !Point methodsFor: 'printing'!
  1054. printOn: aStream
  1055. "Print receiver in classic x@y notation."
  1056. x printOn: aStream.
  1057. aStream nextPutAll: '@'.
  1058. (y notNil and: [ y negative ]) ifTrue: [
  1059. "Avoid ambiguous @- construct"
  1060. aStream space ].
  1061. y printOn: aStream
  1062. ! !
  1063. !Point methodsFor: 'rectangle creation'!
  1064. corner: aPoint
  1065. ^ Rectangle origin: self corner: aPoint
  1066. !
  1067. extent: aPoint
  1068. ^ Rectangle origin: self extent: aPoint
  1069. !
  1070. rectangle: aPoint
  1071. ^ Rectangle point: self point: aPoint
  1072. ! !
  1073. !Point methodsFor: 'transforming'!
  1074. dist: aPoint
  1075. "Answer the distance between aPoint and the receiver."
  1076. | dx dy |
  1077. dx := aPoint x - x.
  1078. dy := aPoint y - y.
  1079. ^ (dx * dx + (dy * dy)) sqrt
  1080. !
  1081. translateBy: delta
  1082. "Answer a Point translated by delta (an instance of Point)."
  1083. ^ (delta x + x) @ (delta y + y)
  1084. ! !
  1085. !Point class methodsFor: 'accessing'!
  1086. classTag
  1087. "Returns a tag or general category for this class.
  1088. Typically used to help tools do some reflection.
  1089. Helios, for example, uses this to decide what icon the class should display."
  1090. ^ 'magnitude'
  1091. ! !
  1092. !Point class methodsFor: 'instance creation'!
  1093. x: aNumber y: anotherNumber
  1094. ^ self new
  1095. x: aNumber;
  1096. y: anotherNumber;
  1097. yourself
  1098. ! !
  1099. Object subclass: #Random
  1100. slots: {}
  1101. package: 'Kernel-Objects'!
  1102. !Random commentStamp!
  1103. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  1104. ## API
  1105. The typical use case it to use the `#next` method like the following:
  1106. Random new next
  1107. 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`
  1108. 10 atRandom
  1109. A random number in a specific interval can be obtained with the following:
  1110. (3 to: 7) atRandom
  1111. 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:
  1112. 5 atRandom + 2
  1113. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1114. #('a' 'b' 'c') atRandom
  1115. As well as letter from a `String`:
  1116. 'abc' atRandom
  1117. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1118. !Random methodsFor: 'accessing'!
  1119. next
  1120. <inlineJS: 'return Math.random()'>
  1121. !
  1122. next: anInteger
  1123. ^ (1 to: anInteger) collect: [ :each | self next ]
  1124. ! !
  1125. Object subclass: #Rectangle
  1126. slots: {#origin. #corner}
  1127. package: 'Kernel-Objects'!
  1128. !Rectangle commentStamp!
  1129. I represent a Rectangle defined by my two corners.
  1130. The simplest way to create an instance is using Point methods:
  1131. 1@1 corner: 2@2
  1132. WIll create a rectangle with 1@1 as the top left and 2@2 at the bottom right.
  1133. 1@1 extent: 1@1
  1134. Will create the same rectangle, defining an origin and a size instead of an origin and a corner.!
  1135. !Rectangle methodsFor: 'accessing'!
  1136. corner
  1137. ^ corner
  1138. !
  1139. origin
  1140. ^ origin
  1141. ! !
  1142. !Rectangle methodsFor: 'private'!
  1143. setPoint: pt1 point: pt2
  1144. origin := (pt1 x min: pt2 x)@(pt1 y min: pt2 y).
  1145. corner := (pt1 x max: pt2 x)@(pt1 y max: pt2 y).
  1146. ! !
  1147. !Rectangle methodsFor: 'testing'!
  1148. = aRectangle
  1149. ^ origin = aRectangle origin and: [ corner = aRectangle corner ]
  1150. !
  1151. containsPoint: aPoint
  1152. ^ origin <= aPoint and: [ corner >= aPoint ]
  1153. !
  1154. containsRect: aRect
  1155. ^ aRect origin >= origin and: [ aRect corner <= corner ]
  1156. !
  1157. printOn: aStream
  1158. origin printOn: aStream.
  1159. aStream nextPutAll: ' corner: '.
  1160. corner printOn: aStream.
  1161. ! !
  1162. !Rectangle class methodsFor: 'instance creation'!
  1163. origin: anOrigin corner: aCorner
  1164. ^ self basicNew setPoint: anOrigin point: aCorner.
  1165. !
  1166. origin: anOrigin extent: anExtent
  1167. ^ self basicNew setPoint: anOrigin point: anOrigin + anExtent.
  1168. !
  1169. point: anOrigin point: aCorner
  1170. ^ self basicNew setPoint: anOrigin point: aCorner.
  1171. ! !
  1172. Object subclass: #UndefinedObject
  1173. slots: {}
  1174. package: 'Kernel-Objects'!
  1175. !UndefinedObject commentStamp!
  1176. 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.
  1177. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  1178. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  1179. !UndefinedObject methodsFor: 'accessing'!
  1180. identityHash
  1181. ^ 'NIL'
  1182. ! !
  1183. !UndefinedObject methodsFor: 'converting'!
  1184. asJavaScriptObject
  1185. ^ null
  1186. ! !
  1187. !UndefinedObject methodsFor: 'copying'!
  1188. deepCopy
  1189. ^ self
  1190. !
  1191. shallowCopy
  1192. ^ self
  1193. ! !
  1194. !UndefinedObject methodsFor: 'evaluating'!
  1195. value
  1196. <inlineJS: 'return null'>
  1197. ! !
  1198. !UndefinedObject methodsFor: 'printing'!
  1199. printOn: aStream
  1200. aStream nextPutAll: 'nil'
  1201. ! !
  1202. !UndefinedObject methodsFor: 'testing'!
  1203. == anObject
  1204. ^ anObject isNil
  1205. !
  1206. ifNil: aBlock
  1207. "inlined in the Compiler"
  1208. ^ self ifNil: aBlock ifNotNil: []
  1209. !
  1210. ifNil: aBlock ifNotNil: anotherBlock
  1211. "inlined in the Compiler"
  1212. ^ aBlock value
  1213. !
  1214. ifNotNil: aBlock
  1215. "inlined in the Compiler"
  1216. ^ self
  1217. !
  1218. ifNotNil: aBlock ifNil: anotherBlock
  1219. "inlined in the Compiler"
  1220. ^ anotherBlock value
  1221. !
  1222. isImmutable
  1223. ^ true
  1224. !
  1225. isNil
  1226. ^ true
  1227. !
  1228. notNil
  1229. ^ false
  1230. ! !
  1231. !UndefinedObject class methodsFor: 'instance creation'!
  1232. new
  1233. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1234. ! !
  1235. Object setTraitComposition: {TIsInGroup} asTraitComposition!
  1236. UndefinedObject setTraitComposition: {TSubclassable} asTraitComposition!
  1237. ! !