Kernel-Objects.st 23 KB

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