2
0

Kernel-Objects.st 20 KB

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