1
0

Kernel-Objects.st 22 KB

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