2
0

Kernel-Objects.st 23 KB

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