Kernel-Objects.st 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. return aBoolean !!= null &&
  329. typeof aBoolean._isBoolean === "function" &&
  330. aBoolean._isBoolean() &&
  331. Boolean(self == true) == aBoolean
  332. >
  333. !
  334. == aBoolean
  335. ^ self = aBoolean
  336. ! !
  337. !Boolean methodsFor: 'controlling'!
  338. & aBoolean
  339. <
  340. if(self == true) {
  341. return aBoolean;
  342. } else {
  343. return false;
  344. }
  345. >
  346. !
  347. and: aBlock
  348. ^ self = true
  349. ifTrue: aBlock
  350. ifFalse: [ false ]
  351. !
  352. ifFalse: aBlock
  353. "inlined in the Compiler"
  354. ^ self ifTrue: [] ifFalse: aBlock
  355. !
  356. ifFalse: aBlock ifTrue: anotherBlock
  357. "inlined in the Compiler"
  358. ^ self ifTrue: anotherBlock ifFalse: aBlock
  359. !
  360. ifTrue: aBlock
  361. "inlined in the Compiler"
  362. ^ self ifTrue: aBlock ifFalse: []
  363. !
  364. ifTrue: aBlock ifFalse: anotherBlock
  365. "inlined in the Compiler"
  366. <
  367. if(self == true) {
  368. return aBlock._value();
  369. } else {
  370. return anotherBlock._value();
  371. }
  372. >
  373. !
  374. not
  375. ^ self = false
  376. !
  377. or: aBlock
  378. ^ self = true
  379. ifTrue: [ true ]
  380. ifFalse: aBlock
  381. !
  382. | aBoolean
  383. <
  384. if(self == true) {
  385. return true;
  386. } else {
  387. return aBoolean;
  388. }
  389. >
  390. ! !
  391. !Boolean methodsFor: 'converting'!
  392. asBit
  393. ^ self ifTrue: [ 1 ] ifFalse: [ 0 ]
  394. !
  395. asJSON
  396. ^ self
  397. !
  398. asString
  399. < return self.toString() >
  400. ! !
  401. !Boolean methodsFor: 'copying'!
  402. deepCopy
  403. ^ self
  404. !
  405. shallowCopy
  406. ^ self
  407. ! !
  408. !Boolean methodsFor: 'printing'!
  409. printOn: aStream
  410. aStream nextPutAll: self asString
  411. ! !
  412. !Boolean methodsFor: 'testing'!
  413. isBoolean
  414. ^ true
  415. !
  416. isImmutable
  417. ^ true
  418. ! !
  419. Object subclass: #Date
  420. instanceVariableNames: ''
  421. package: 'Kernel-Objects'!
  422. !Date commentStamp!
  423. I am used to work with both dates and times. Therefore `Date today` and `Date now` are both valid in
  424. Amber and answer the same date object.
  425. Date directly maps to the `Date()` JavaScript constructor, and Amber date objects are JavaScript date objects.
  426. ## API
  427. The class-side `instance creation` protocol contains some convenience methods for creating date/time objects such as `#fromSeconds:`.
  428. Arithmetic and comparison is supported (see the `comparing` and `arithmetic` protocols).
  429. The `converting` protocol provides convenience methods for various convertions (to numbers, strings, etc.).!
  430. !Date methodsFor: 'accessing'!
  431. day
  432. ^ self dayOfWeek
  433. !
  434. day: aNumber
  435. self dayOfWeek: aNumber
  436. !
  437. dayOfMonth
  438. <return self.getDate()>
  439. !
  440. dayOfMonth: aNumber
  441. <self.setDate(aNumber)>
  442. !
  443. dayOfWeek
  444. <return self.getDay() + 1>
  445. !
  446. dayOfWeek: aNumber
  447. <return self.setDay(aNumber - 1)>
  448. !
  449. hours
  450. <return self.getHours()>
  451. !
  452. hours: aNumber
  453. <self.setHours(aNumber)>
  454. !
  455. milliseconds
  456. <return self.getMilliseconds()>
  457. !
  458. milliseconds: aNumber
  459. <self.setMilliseconds(aNumber)>
  460. !
  461. minutes
  462. <return self.getMinutes()>
  463. !
  464. minutes: aNumber
  465. <self.setMinutes(aNumber)>
  466. !
  467. month
  468. <return self.getMonth() + 1>
  469. !
  470. month: aNumber
  471. <self.setMonth(aNumber - 1)>
  472. !
  473. seconds
  474. <return self.getSeconds()>
  475. !
  476. seconds: aNumber
  477. <self.setSeconds(aNumber)>
  478. !
  479. time
  480. <return self.getTime()>
  481. !
  482. time: aNumber
  483. <self.setTime(aNumber)>
  484. !
  485. year
  486. <return self.getFullYear()>
  487. !
  488. year: aNumber
  489. <self.setFullYear(aNumber)>
  490. ! !
  491. !Date methodsFor: 'arithmetic'!
  492. + aDate
  493. <return self + aDate>
  494. !
  495. - aDate
  496. <return self - aDate>
  497. ! !
  498. !Date methodsFor: 'comparing'!
  499. < aDate
  500. <return self < aDate>
  501. !
  502. <= aDate
  503. <return self <= aDate>
  504. !
  505. > aDate
  506. <return self >> aDate>
  507. !
  508. >= aDate
  509. <return self >>= aDate>
  510. ! !
  511. !Date methodsFor: 'converting'!
  512. asDateString
  513. <return self.toDateString()>
  514. !
  515. asLocaleString
  516. <return self.toLocaleString()>
  517. !
  518. asMilliseconds
  519. ^ self time
  520. !
  521. asNumber
  522. ^ self asMilliseconds
  523. !
  524. asString
  525. <return self.toString()>
  526. !
  527. asTimeString
  528. <return self.toTimeString()>
  529. ! !
  530. !Date methodsFor: 'printing'!
  531. printOn: aStream
  532. aStream nextPutAll: self asString
  533. ! !
  534. !Date class methodsFor: 'accessing'!
  535. classTag
  536. "Returns a tag or general category for this class.
  537. Typically used to help tools do some reflection.
  538. Helios, for example, uses this to decide what icon the class should display."
  539. ^ 'magnitude'
  540. ! !
  541. !Date class methodsFor: 'instance creation'!
  542. fromMilliseconds: aNumber
  543. ^ self new: aNumber
  544. !
  545. fromSeconds: aNumber
  546. ^ self fromMilliseconds: aNumber * 1000
  547. !
  548. fromString: aString
  549. "Example: Date fromString('2011/04/15 00:00:00')"
  550. ^ self new: aString
  551. !
  552. millisecondsToRun: aBlock
  553. | t |
  554. t := Date now.
  555. aBlock value.
  556. ^ Date now - t
  557. !
  558. new: anObject
  559. <return new Date(anObject)>
  560. !
  561. now
  562. ^ self today
  563. !
  564. today
  565. ^ self new
  566. ! !
  567. Object subclass: #Number
  568. instanceVariableNames: ''
  569. package: 'Kernel-Objects'!
  570. !Number commentStamp!
  571. I am the Amber representation for all numbers.
  572. I am directly mapped to JavaScript Number.
  573. ## API
  574. I provide all necessary methods for arithmetic operations, comparison, conversion and so on with numbers.
  575. My instances can also be used to evaluate a block a fixed number of times:
  576. 5 timesRepeat: [ Transcript show: 'This will be printed 5 times'; cr ].
  577. 1 to: 5 do: [ :aNumber| Transcript show: aNumber asString; cr ].
  578. 1 to: 10 by: 2 do: [ :aNumber| Transcript show: aNumber asString; cr ].!
  579. !Number methodsFor: 'accessing'!
  580. identityHash
  581. ^ self asString, 'n'
  582. ! !
  583. !Number methodsFor: 'arithmetic'!
  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. "Inlined in the Compiler"
  594. <return self - aNumber>
  595. !
  596. / aNumber
  597. "Inlined in the Compiler"
  598. <return self / aNumber>
  599. !
  600. // aNumber
  601. ^ (self / aNumber) floor
  602. !
  603. \\ aNumber
  604. <return self % aNumber>
  605. !
  606. abs
  607. <return Math.abs(self);>
  608. !
  609. max: aNumber
  610. <return Math.max(self, aNumber);>
  611. !
  612. min: aNumber
  613. <return Math.min(self, aNumber);>
  614. !
  615. negated
  616. ^ 0 - self
  617. ! !
  618. !Number methodsFor: 'comparing'!
  619. < aNumber
  620. "Inlined in the Compiler"
  621. <return self < aNumber>
  622. !
  623. <= aNumber
  624. "Inlined in the Compiler"
  625. <return self <= aNumber>
  626. !
  627. = aNumber
  628. <
  629. return aNumber !!= null &&
  630. typeof aNumber._isNumber === "function" &&
  631. aNumber._isNumber() &&
  632. Number(self) == aNumber
  633. >
  634. !
  635. > aNumber
  636. "Inlined in the Compiler"
  637. <return self >> aNumber>
  638. !
  639. >= aNumber
  640. "Inlined in the Compiler"
  641. <return self >>= aNumber>
  642. ! !
  643. !Number methodsFor: 'converting'!
  644. & aNumber
  645. <return self & aNumber>
  646. !
  647. @ aNumber
  648. ^ Point x: self y: aNumber
  649. !
  650. asJSON
  651. ^ self
  652. !
  653. asJavascript
  654. ^ '(', self printString, ')'
  655. !
  656. asNumber
  657. ^ self
  658. !
  659. asPoint
  660. ^ Point x: self y: self
  661. !
  662. asString
  663. < return String(self) >
  664. !
  665. atRandom
  666. ^ (Random new next * self) truncated + 1
  667. !
  668. ceiling
  669. <return Math.ceil(self);>
  670. !
  671. floor
  672. <return Math.floor(self);>
  673. !
  674. rounded
  675. <return Math.round(self);>
  676. !
  677. to: aNumber
  678. | array first last count |
  679. first := self truncated.
  680. last := aNumber truncated + 1.
  681. count := 1.
  682. array := Array new.
  683. (last - first) timesRepeat: [
  684. array at: count put: first.
  685. count := count + 1.
  686. first := first + 1 ].
  687. ^ array
  688. !
  689. to: stop by: step
  690. | array value pos |
  691. value := self.
  692. array := Array new.
  693. pos := 1.
  694. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  695. step < 0
  696. ifTrue: [ [ value >= stop ] whileTrue: [
  697. array at: pos put: value.
  698. pos := pos + 1.
  699. value := value + step ]]
  700. ifFalse: [ [ value <= stop ] whileTrue: [
  701. array at: pos put: value.
  702. pos := pos + 1.
  703. value := value + step ]].
  704. ^ array
  705. !
  706. truncated
  707. <
  708. if(self >>= 0) {
  709. return Math.floor(self);
  710. } else {
  711. return Math.floor(self * (-1)) * (-1);
  712. };
  713. >
  714. !
  715. | aNumber
  716. <return self | aNumber>
  717. ! !
  718. !Number methodsFor: 'copying'!
  719. copy
  720. ^ self
  721. !
  722. deepCopy
  723. ^ self copy
  724. ! !
  725. !Number methodsFor: 'enumerating'!
  726. timesRepeat: aBlock
  727. | count |
  728. count := 1.
  729. [ count > self ] whileFalse: [
  730. aBlock value.
  731. count := count + 1 ]
  732. !
  733. to: stop by: step do: aBlock
  734. | value |
  735. value := self.
  736. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  737. step < 0
  738. ifTrue: [ [ value >= stop ] whileTrue: [
  739. aBlock value: value.
  740. value := value + step ]]
  741. ifFalse: [ [ value <= stop ] whileTrue: [
  742. aBlock value: value.
  743. value := value + step ]]
  744. !
  745. to: stop do: aBlock
  746. "Evaluate aBlock for each number from self to aNumber."
  747. | nextValue |
  748. nextValue := self.
  749. [ nextValue <= stop ]
  750. whileTrue:
  751. [ aBlock value: nextValue.
  752. nextValue := nextValue + 1 ]
  753. ! !
  754. !Number methodsFor: 'mathematical functions'!
  755. ** exponent
  756. ^ self raisedTo: exponent
  757. !
  758. arcCos
  759. <return Math.acos(self);>
  760. !
  761. arcSin
  762. <return Math.asin(self);>
  763. !
  764. arcTan
  765. <return Math.atan(self);>
  766. !
  767. cos
  768. <return Math.cos(self);>
  769. !
  770. ln
  771. <return Math.log(self);>
  772. !
  773. log
  774. <return Math.log(self) / Math.LN10;>
  775. !
  776. log: aNumber
  777. <return Math.log(self) / Math.log(aNumber);>
  778. !
  779. raisedTo: exponent
  780. <return Math.pow(self, exponent);>
  781. !
  782. sign
  783. self isZero
  784. ifTrue: [ ^ 0 ].
  785. self positive
  786. ifTrue: [ ^ 1 ]
  787. ifFalse: [ ^ -1 ].
  788. !
  789. sin
  790. <return Math.sin(self);>
  791. !
  792. sqrt
  793. <return Math.sqrt(self)>
  794. !
  795. squared
  796. ^ self * self
  797. !
  798. tan
  799. <return Math.tan(self);>
  800. ! !
  801. !Number methodsFor: 'printing'!
  802. printOn: aStream
  803. aStream nextPutAll: self asString
  804. !
  805. printShowingDecimalPlaces: placesDesired
  806. <return self.toFixed(placesDesired)>
  807. ! !
  808. !Number methodsFor: 'testing'!
  809. even
  810. ^ 0 = (self \\ 2)
  811. !
  812. isImmutable
  813. ^ true
  814. !
  815. isNumber
  816. ^ true
  817. !
  818. isZero
  819. ^ self = 0
  820. !
  821. negative
  822. "Answer whether the receiver is mathematically negative."
  823. ^ self < 0
  824. !
  825. odd
  826. ^ self even not
  827. !
  828. positive
  829. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  830. ^ self >= 0
  831. ! !
  832. !Number class methodsFor: 'accessing'!
  833. classTag
  834. "Returns a tag or general category for this class.
  835. Typically used to help tools do some reflection.
  836. Helios, for example, uses this to decide what icon the class should display."
  837. ^ 'magnitude'
  838. ! !
  839. !Number class methodsFor: 'instance creation'!
  840. e
  841. <return Math.E;>
  842. !
  843. pi
  844. <return Math.PI>
  845. ! !
  846. Object subclass: #Point
  847. instanceVariableNames: 'x y'
  848. package: 'Kernel-Objects'!
  849. !Point commentStamp!
  850. I represent an x-y pair of numbers usually designating a geometric coordinate.
  851. ## API
  852. Instances are traditionally created using the binary `#@` message to a number:
  853. 100@120
  854. Points can then be arithmetically manipulated:
  855. 100@100 + (10@10)
  856. ...or for example:
  857. (100@100) * 2
  858. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  859. 100@ -100 "but 100@-100 would not parse"!
  860. !Point methodsFor: 'accessing'!
  861. x
  862. ^ x
  863. !
  864. x: aNumber
  865. x := aNumber
  866. !
  867. y
  868. ^ y
  869. !
  870. y: aNumber
  871. y := aNumber
  872. ! !
  873. !Point methodsFor: 'arithmetic'!
  874. * aPoint
  875. ^ Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  876. !
  877. + aPoint
  878. ^ Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  879. !
  880. - aPoint
  881. ^ Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  882. !
  883. / aPoint
  884. ^ Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  885. ! !
  886. !Point methodsFor: 'comparing'!
  887. < aPoint
  888. ^ self x < aPoint x and: [
  889. self y < aPoint y ]
  890. !
  891. <= aPoint
  892. ^ self x <= aPoint x and: [
  893. self y <= aPoint y ]
  894. !
  895. = aPoint
  896. ^ aPoint class = self class and: [
  897. (aPoint x = self x) & (aPoint y = self y) ]
  898. !
  899. > aPoint
  900. ^ self x > aPoint x and: [
  901. self y > aPoint y ]
  902. !
  903. >= aPoint
  904. ^ self x >= aPoint x and: [
  905. self y >= aPoint y ]
  906. ! !
  907. !Point methodsFor: 'converting'!
  908. asPoint
  909. ^ self
  910. ! !
  911. !Point methodsFor: 'printing'!
  912. printOn: aStream
  913. "Print receiver in classic x@y notation."
  914. x printOn: aStream.
  915. aStream nextPutAll: '@'.
  916. (y notNil and: [ y negative ]) ifTrue: [
  917. "Avoid ambiguous @- construct"
  918. aStream space ].
  919. y printOn: aStream
  920. ! !
  921. !Point methodsFor: 'transforming'!
  922. dist: aPoint
  923. "Answer the distance between aPoint and the receiver."
  924. | dx dy |
  925. dx := aPoint x - x.
  926. dy := aPoint y - y.
  927. ^ (dx * dx + (dy * dy)) sqrt
  928. !
  929. translateBy: delta
  930. "Answer a Point translated by delta (an instance of Point)."
  931. ^ (delta x + x) @ (delta y + y)
  932. ! !
  933. !Point class methodsFor: 'accessing'!
  934. classTag
  935. "Returns a tag or general category for this class.
  936. Typically used to help tools do some reflection.
  937. Helios, for example, uses this to decide what icon the class should display."
  938. ^ 'magnitude'
  939. ! !
  940. !Point class methodsFor: 'instance creation'!
  941. x: aNumber y: anotherNumber
  942. ^ self new
  943. x: aNumber;
  944. y: anotherNumber;
  945. yourself
  946. ! !
  947. Object subclass: #Random
  948. instanceVariableNames: ''
  949. package: 'Kernel-Objects'!
  950. !Random commentStamp!
  951. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  952. ## API
  953. The typical use case it to use the `#next` method like the following:
  954. Random new next
  955. 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`
  956. 10 atRandom
  957. A random number in a specific interval can be obtained with the following:
  958. (3 to: 7) atRandom
  959. 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:
  960. 5 atRandom + 2
  961. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  962. #('a' 'b' 'c') atRandom
  963. As well as letter from a `String`:
  964. 'abc' atRandom
  965. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  966. !Random methodsFor: 'accessing'!
  967. next
  968. <return Math.random()>
  969. !
  970. next: anInteger
  971. ^ (1 to: anInteger) collect: [ :each | self next ]
  972. ! !
  973. Object subclass: #UndefinedObject
  974. instanceVariableNames: ''
  975. package: 'Kernel-Objects'!
  976. !UndefinedObject commentStamp!
  977. 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.
  978. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  979. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  980. !UndefinedObject methodsFor: 'class creation'!
  981. subclass: aString instanceVariableNames: anotherString
  982. "Kept for file-in compatibility."
  983. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  984. !
  985. subclass: aString instanceVariableNames: aString2 category: aString3
  986. "Kept for file-in compatibility."
  987. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  988. !
  989. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  990. "Kept for file-in compatibility. ignores class variables and pools."
  991. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  992. !
  993. subclass: aString instanceVariableNames: aString2 package: aString3
  994. ^ ClassBuilder new
  995. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  996. ! !
  997. !UndefinedObject methodsFor: 'converting'!
  998. asJSON
  999. ^ null
  1000. ! !
  1001. !UndefinedObject methodsFor: 'copying'!
  1002. deepCopy
  1003. ^ self
  1004. !
  1005. shallowCopy
  1006. ^ self
  1007. ! !
  1008. !UndefinedObject methodsFor: 'printing'!
  1009. printOn: aStream
  1010. aStream nextPutAll: 'nil'
  1011. ! !
  1012. !UndefinedObject methodsFor: 'testing'!
  1013. ifNil: aBlock
  1014. "inlined in the Compiler"
  1015. ^ self ifNil: aBlock ifNotNil: []
  1016. !
  1017. ifNil: aBlock ifNotNil: anotherBlock
  1018. "inlined in the Compiler"
  1019. ^ aBlock value
  1020. !
  1021. ifNotNil: aBlock
  1022. "inlined in the Compiler"
  1023. ^ self
  1024. !
  1025. ifNotNil: aBlock ifNil: anotherBlock
  1026. "inlined in the Compiler"
  1027. ^ anotherBlock value
  1028. !
  1029. isImmutable
  1030. ^ true
  1031. !
  1032. isNil
  1033. ^ true
  1034. !
  1035. notNil
  1036. ^ false
  1037. ! !
  1038. !UndefinedObject class methodsFor: 'instance creation'!
  1039. new
  1040. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1041. ! !