Kernel-Objects.st 24 KB

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