Kernel-Objects.st 23 KB

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