Kernel-Objects.st 26 KB

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