Kernel-Objects.st 20 KB

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