Kernel-Objects.st 23 KB

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