Kernel-Objects.st 20 KB

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