Kernel-Objects.st 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. Smalltalk current createPackage: 'Kernel-Objects' properties: #{}!
  2. nil subclass: #Object
  3. instanceVariableNames: ''
  4. package: 'Kernel-Objects'!
  5. !Object commentStamp!
  6. *Object is the root of the Smalltalk class system*. All classes in the system are subclasses of Object.
  7. Object provides default behavior common to all normal objects, such as:
  8. - access
  9. - copying
  10. - comparison
  11. - error handling
  12. - message sending
  13. - reflection
  14. Also utility messages that all objects should respond to are defined here.
  15. Object has no instance variable.
  16. ##Access
  17. Instance variables can be accessed with `#instVarAt:` and `#instVarAt:put:`. `Object >> instanceVariableNames` answers a collection of all instance variable names.
  18. Accessing JavaScript properties of an object is done through `#basicAt:`, `#basicAt:put:` and `basicDelete:`.
  19. ##Copying
  20. 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.
  21. 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.
  22. ##Comparison
  23. Objects understand equality `#=` and identity `#==` comparison.
  24. ##Error handling
  25. - `#halt` is the typical message to use for inserting breakpoints during debugging.
  26. - `#error:` throws a generic error exception
  27. - `#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.
  28. Overriding this message can be useful to implement proxies for example.!
  29. !Object methodsFor: 'accessing'!
  30. basicAt: aString
  31. <return self[aString]>
  32. !
  33. basicAt: aString put: anObject
  34. <return self[aString] = anObject>
  35. !
  36. basicDelete: aString
  37. <delete self[aString]; return aString>
  38. !
  39. class
  40. <return self.klass>
  41. !
  42. identityHash
  43. <
  44. var hash=self.identityHash;
  45. if (hash) return hash;
  46. hash=smalltalk.nextId();
  47. Object.defineProperty(self, 'identityHash', {value:hash});
  48. return hash;
  49. >
  50. !
  51. instVarAt: aSymbol
  52. <return self['@'+aSymbol._asString()]>
  53. !
  54. instVarAt: aSymbol put: anObject
  55. <self['@' + aSymbol._asString()] = anObject>
  56. !
  57. size
  58. self error: 'Object not indexable'
  59. !
  60. value
  61. <return self.valueOf()>
  62. !
  63. yourself
  64. ^self
  65. ! !
  66. !Object methodsFor: 'comparing'!
  67. = anObject
  68. ^self == anObject
  69. !
  70. == anObject
  71. ^self identityHash = anObject identityHash
  72. !
  73. ~= anObject
  74. ^(self = anObject) = false
  75. !
  76. ~~ anObject
  77. ^(self == anObject) = false
  78. ! !
  79. !Object methodsFor: 'converting'!
  80. -> anObject
  81. ^Association key: self value: anObject
  82. !
  83. asJSON
  84. | variables |
  85. variables := HashedCollection new.
  86. self class allInstanceVariableNames do: [:each |
  87. variables at: each put: (self instVarAt: each) asJSON].
  88. ^variables
  89. !
  90. asJSONString
  91. ^JSON stringify: self asJSON
  92. !
  93. asJavascript
  94. ^self asString
  95. !
  96. asString
  97. ^self printString
  98. ! !
  99. !Object methodsFor: 'copying'!
  100. copy
  101. ^self shallowCopy postCopy
  102. !
  103. deepCopy
  104. <
  105. var copy = self.klass._new();
  106. for(var i in self) {
  107. if(/^@.+/.test(i)) {
  108. copy[i] = self[i]._deepCopy();
  109. }
  110. }
  111. return copy;
  112. >
  113. !
  114. postCopy
  115. !
  116. shallowCopy
  117. <
  118. var copy = self.klass._new();
  119. for(var i in self) {
  120. if(/^@.+/.test(i)) {
  121. copy[i] = self[i];
  122. }
  123. }
  124. return copy;
  125. >
  126. ! !
  127. !Object methodsFor: 'error handling'!
  128. deprecatedAPI
  129. "Just a simple way to deprecate methods.
  130. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  131. but it could in the future."
  132. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'
  133. !
  134. doesNotUnderstand: aMessage
  135. MessageNotUnderstood new
  136. receiver: self;
  137. message: aMessage;
  138. signal
  139. !
  140. error: aString
  141. Error signal: aString
  142. !
  143. halt
  144. self error: 'Halt encountered'
  145. !
  146. shouldNotImplement
  147. self error: 'This method should not be implemented in ', self class name
  148. !
  149. subclassResponsibility
  150. self error: 'This method is a responsibility of a subclass'
  151. !
  152. throw: anObject
  153. < throw anObject >
  154. !
  155. try: aBlock catch: anotherBlock
  156. <try{return aBlock()} catch(e) {return anotherBlock(e)}>
  157. ! !
  158. !Object methodsFor: 'initialization'!
  159. initialize
  160. ! !
  161. !Object methodsFor: 'message handling'!
  162. basicPerform: aSymbol
  163. ^self basicPerform: aSymbol withArguments: #()
  164. !
  165. basicPerform: aSymbol withArguments: aCollection
  166. <return self[aSymbol].apply(self, aCollection);>
  167. !
  168. perform: aSymbol
  169. ^self perform: aSymbol withArguments: #()
  170. !
  171. perform: aSymbol withArguments: aCollection
  172. <return smalltalk.send(self, aSymbol._asSelector(), aCollection)>
  173. ! !
  174. !Object methodsFor: 'printing'!
  175. log: aString block: aBlock
  176. | result |
  177. console log: aString, ' time: ', (Date millisecondsToRun: [result := aBlock value]) printString.
  178. ^result
  179. !
  180. printNl
  181. <console.log(self)>
  182. !
  183. printString
  184. ^'a ', self class name
  185. !
  186. storeOn: aStream
  187. aStream nextPutAll: self printString
  188. !
  189. storeString
  190. "Answer a String representation of the receiver from which the receiver
  191. can be reconstructed."
  192. ^ String streamContents: [:s | self storeOn: s]
  193. ! !
  194. !Object methodsFor: 'testing'!
  195. ifNil: aBlock
  196. "inlined in the Compiler"
  197. ^self
  198. !
  199. ifNil: aBlock ifNotNil: anotherBlock
  200. "inlined in the Compiler"
  201. ^anotherBlock value
  202. !
  203. ifNotNil: aBlock
  204. "inlined in the Compiler"
  205. ^aBlock value
  206. !
  207. ifNotNil: aBlock ifNil: anotherBlock
  208. "inlined in the Compiler"
  209. ^aBlock value
  210. !
  211. isBoolean
  212. ^ false
  213. !
  214. isClass
  215. ^false
  216. !
  217. isKindOf: aClass
  218. ^(self isMemberOf: aClass)
  219. ifTrue: [true]
  220. ifFalse: [self class inheritsFrom: aClass]
  221. !
  222. isMemberOf: aClass
  223. ^self class = aClass
  224. !
  225. isMetaclass
  226. ^false
  227. !
  228. isNil
  229. ^false
  230. !
  231. isNumber
  232. ^false
  233. !
  234. isParseFailure
  235. ^false
  236. !
  237. isString
  238. ^false
  239. !
  240. isSymbol
  241. ^false
  242. !
  243. notNil
  244. ^self isNil not
  245. !
  246. respondsTo: aSelector
  247. ^self class canUnderstand: aSelector
  248. ! !
  249. !Object class methodsFor: 'initialization'!
  250. initialize
  251. "no op"
  252. ! !
  253. Object subclass: #Boolean
  254. instanceVariableNames: ''
  255. package: 'Kernel-Objects'!
  256. !Boolean commentStamp!
  257. Boolean wraps the JavaScript `Boolean()` constructor. The `true` and `false` objects are the JavaScript boolean objects.
  258. Boolean defines the protocol for logic testing operations and conditional control structures for the logical values.
  259. Boolean instances are weither `true` or `false`.!
  260. !Boolean methodsFor: 'comparing'!
  261. = aBoolean
  262. <
  263. if(!! aBoolean._isBoolean || !! aBoolean._isBoolean()) {
  264. return false;
  265. }
  266. return Boolean(self == true) == aBoolean
  267. >
  268. !
  269. == aBoolean
  270. ^self = aBoolean
  271. ! !
  272. !Boolean methodsFor: 'controlling'!
  273. & aBoolean
  274. <
  275. if(self == true) {
  276. return aBoolean;
  277. } else {
  278. return false;
  279. }
  280. >
  281. !
  282. and: aBlock
  283. ^self = true
  284. ifTrue: aBlock
  285. ifFalse: [false]
  286. !
  287. ifFalse: aBlock
  288. "inlined in the Compiler"
  289. ^self ifTrue: [] ifFalse: aBlock
  290. !
  291. ifFalse: aBlock ifTrue: anotherBlock
  292. "inlined in the Compiler"
  293. ^self ifTrue: anotherBlock ifFalse: aBlock
  294. !
  295. ifTrue: aBlock
  296. "inlined in the Compiler"
  297. ^self ifTrue: aBlock ifFalse: []
  298. !
  299. ifTrue: aBlock ifFalse: anotherBlock
  300. "inlined in the Compiler"
  301. <
  302. if(self == true) {
  303. return aBlock();
  304. } else {
  305. return anotherBlock();
  306. }
  307. >
  308. !
  309. not
  310. ^self = false
  311. !
  312. or: aBlock
  313. ^self = true
  314. ifTrue: [true]
  315. ifFalse: aBlock
  316. !
  317. | aBoolean
  318. <
  319. if(self == true) {
  320. return true;
  321. } else {
  322. return aBoolean;
  323. }
  324. >
  325. ! !
  326. !Boolean methodsFor: 'converting'!
  327. asJSON
  328. ^self
  329. ! !
  330. !Boolean methodsFor: 'copying'!
  331. deepCopy
  332. ^self
  333. !
  334. shallowCopy
  335. ^self
  336. ! !
  337. !Boolean methodsFor: 'printing'!
  338. printString
  339. <return self.toString()>
  340. ! !
  341. !Boolean methodsFor: 'testing'!
  342. isBoolean
  343. ^ true
  344. ! !
  345. Object subclass: #Date
  346. instanceVariableNames: ''
  347. package: 'Kernel-Objects'!
  348. !Date commentStamp!
  349. The Date class is used to work with dates and times. Therefore `Date today` and `Date now` are both valid in
  350. Amber and answer the same date object.
  351. Date wraps the `Date()` JavaScript constructor, and Smalltalk date objects are JavaScript date objects.!
  352. !Date methodsFor: 'accessing'!
  353. day
  354. ^self dayOfWeek
  355. !
  356. day: aNumber
  357. self dayOfWeek: aNumber
  358. !
  359. dayOfMonth
  360. <return self.getDate()>
  361. !
  362. dayOfMonth: aNumber
  363. <self.setDate(aNumber)>
  364. !
  365. dayOfWeek
  366. <return self.getDay() + 1>
  367. !
  368. dayOfWeek: aNumber
  369. <return self.setDay(aNumber - 1)>
  370. !
  371. hours
  372. <return self.getHours()>
  373. !
  374. hours: aNumber
  375. <self.setHours(aNumber)>
  376. !
  377. milliseconds
  378. <return self.getMilliseconds()>
  379. !
  380. milliseconds: aNumber
  381. <self.setMilliseconds(aNumber)>
  382. !
  383. minutes
  384. <return self.getMinutes()>
  385. !
  386. minutes: aNumber
  387. <self.setMinutes(aNumber)>
  388. !
  389. month
  390. <return self.getMonth() + 1>
  391. !
  392. month: aNumber
  393. <self.setMonth(aNumber - 1)>
  394. !
  395. seconds
  396. <return self.getSeconds()>
  397. !
  398. seconds: aNumber
  399. <self.setSeconds(aNumber)>
  400. !
  401. time
  402. <return self.getTime()>
  403. !
  404. time: aNumber
  405. <self.setTime(aNumber)>
  406. !
  407. year
  408. <return self.getFullYear()>
  409. !
  410. year: aNumber
  411. <self.setFullYear(aNumber)>
  412. ! !
  413. !Date methodsFor: 'arithmetic'!
  414. + aDate
  415. <return self + aDate>
  416. !
  417. - aDate
  418. <return self - aDate>
  419. ! !
  420. !Date methodsFor: 'comparing'!
  421. < aDate
  422. <return self < aDate>
  423. !
  424. <= aDate
  425. <return self <= aDate>
  426. !
  427. > aDate
  428. <return self >> aDate>
  429. !
  430. >= aDate
  431. <return self >>= aDate>
  432. ! !
  433. !Date methodsFor: 'converting'!
  434. asDateString
  435. <return self.toDateString()>
  436. !
  437. asLocaleString
  438. <return self.toLocaleString()>
  439. !
  440. asMilliseconds
  441. ^self time
  442. !
  443. asNumber
  444. ^self asMilliseconds
  445. !
  446. asString
  447. <return self.toString()>
  448. !
  449. asTimeString
  450. <return self.toTimeString()>
  451. ! !
  452. !Date methodsFor: 'printing'!
  453. printString
  454. ^self asString
  455. ! !
  456. !Date class methodsFor: 'instance creation'!
  457. fromMilliseconds: aNumber
  458. ^self new: aNumber
  459. !
  460. fromSeconds: aNumber
  461. ^self fromMilliseconds: aNumber * 1000
  462. !
  463. fromString: aString
  464. "Example: Date fromString('2011/04/15 00:00:00')"
  465. ^self new: aString
  466. !
  467. millisecondsToRun: aBlock
  468. | t |
  469. t := Date now.
  470. aBlock value.
  471. ^Date now - t
  472. !
  473. new: anObject
  474. <return new Date(anObject)>
  475. !
  476. now
  477. ^self today
  478. !
  479. today
  480. ^self new
  481. ! !
  482. Object subclass: #JSObjectProxy
  483. instanceVariableNames: 'jsObject'
  484. package: 'Kernel-Objects'!
  485. !JSObjectProxy commentStamp!
  486. JSObjectProxy handles sending messages to JavaScript object, therefore accessing JavaScript objects from Amber is transparent.
  487. JSOjbectProxy makes intensive use of `#doesNotUnderstand:`.
  488. ## Examples
  489. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  490. window alert: 'hello world'.
  491. window inspect.
  492. (window jQuery: 'body') append: 'hello world'
  493. Smalltalk messages sends are converted to JavaScript function calls or object property access _(in this order)_. If n one of them match, a `MessageNotUnderstood` error will be thrown.
  494. ## Message conversion rules
  495. - `someUser name` becomes `someUser.name`
  496. - `someUser name: 'John'` becomes `someUser name = "John"`
  497. - `console log: 'hello world'` becomes `console.log('hello world')`
  498. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  499. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  500. !JSObjectProxy methodsFor: 'accessing'!
  501. at: aSymbol
  502. <return self['@jsObject'][aSymbol._asString()]>
  503. !
  504. at: aSymbol put: anObject
  505. <self['@jsObject'][aSymbol._asString()] = anObject>
  506. !
  507. jsObject
  508. ^jsObject
  509. !
  510. jsObject: aJSObject
  511. jsObject := aJSObject
  512. ! !
  513. !JSObjectProxy methodsFor: 'enumerating'!
  514. keysAndValuesDo: aBlock
  515. <
  516. for(var i in self['@jsObject']) {
  517. aBlock._value_(i, self['@jsObject'][i]);
  518. }
  519. >
  520. ! !
  521. !JSObjectProxy methodsFor: 'proxy'!
  522. addObjectVariablesTo: aDictionary
  523. <
  524. for(var i in self['@jsObject']) {
  525. aDictionary._at_put_(i, self['@jsObject'][i]);
  526. }
  527. >
  528. !
  529. doesNotUnderstand: aMessage
  530. ^ (self canForwardMessage: aMessage)
  531. ifTrue: [ self forwardMessage: aMessage ]
  532. ifFalse: [ ^ super doesNotUnderstand: aMessage ]
  533. !
  534. forwardMessage: aMessage
  535. <
  536. return smalltalk.send(self._jsObject(), aMessage._selector()._asJavaScriptSelector(), aMessage._arguments());
  537. >
  538. !
  539. inspectOn: anInspector
  540. | variables |
  541. variables := Dictionary new.
  542. variables at: '#self' put: self jsObject.
  543. anInspector setLabel: self printString.
  544. self addObjectVariablesTo: variables.
  545. anInspector setVariables: variables
  546. !
  547. printString
  548. ^self jsObject toString
  549. ! !
  550. !JSObjectProxy methodsFor: 'testing'!
  551. canForwardMessage: aMessage
  552. <
  553. var jsSelector = aMessage._selector()._asJavaScriptSelector();
  554. if(jsSelector in self._jsObject()) {
  555. return true
  556. } else {
  557. return false;
  558. }
  559. >
  560. ! !
  561. !JSObjectProxy class methodsFor: 'instance creation'!
  562. on: aJSObject
  563. ^self new
  564. jsObject: aJSObject;
  565. yourself
  566. ! !
  567. Object subclass: #Number
  568. instanceVariableNames: ''
  569. package: 'Kernel-Objects'!
  570. !Number commentStamp!
  571. Number holds the most general methods for dealing with numbers.
  572. Number is directly mapped to JavaScript Number.
  573. Most arithmetic methods like `#+` `#/` `#-` `#max:` are directly inlined into javascript.
  574. ##Enumerating
  575. A Number can be used to evaluate a Block a fixed number of times:
  576. 5 timesRepeat: [Transcript show: 'This will be printed 5 times'; cr].
  577. 1 to: 5 do: [:aNumber| Transcript show: aNumber asString; cr].
  578. 1 to: 10 by: 2 do: [:aNumber| Transcript show: aNumber asString; cr].!
  579. !Number methodsFor: 'accessing'!
  580. identityHash
  581. ^self asString, 'n'
  582. ! !
  583. !Number methodsFor: 'arithmetic'!
  584. * aNumber
  585. "Inlined in the Compiler"
  586. <return self * aNumber>
  587. !
  588. + aNumber
  589. "Inlined in the Compiler"
  590. <return self + aNumber>
  591. !
  592. - aNumber
  593. "Inlined in the Compiler"
  594. <return self - aNumber>
  595. !
  596. / aNumber
  597. "Inlined in the Compiler"
  598. <return self / aNumber>
  599. !
  600. \\ aNumber
  601. <return self % aNumber>
  602. !
  603. \\ aNumber
  604. <return self % aNumber>
  605. !
  606. abs
  607. <return Math.abs(self);>
  608. !
  609. max: aNumber
  610. <return Math.max(self, aNumber);>
  611. !
  612. min: aNumber
  613. <return Math.min(self, aNumber);>
  614. !
  615. negated
  616. ^0 - self
  617. !
  618. sqrt
  619. <return Math.sqrt(self)>
  620. !
  621. squared
  622. ^self * self
  623. ! !
  624. !Number methodsFor: 'comparing'!
  625. < aNumber
  626. "Inlined in the Compiler"
  627. <return self < aNumber>
  628. !
  629. <= aNumber
  630. "Inlined in the Compiler"
  631. <return self <= aNumber>
  632. !
  633. = aNumber
  634. <
  635. if(!! aNumber._isNumber || !! aNumber._isNumber()) {
  636. return false;
  637. }
  638. return Number(self) == aNumber
  639. >
  640. !
  641. > aNumber
  642. "Inlined in the Compiler"
  643. <return self >> aNumber>
  644. !
  645. >= aNumber
  646. "Inlined in the Compiler"
  647. <return self >>= aNumber>
  648. ! !
  649. !Number methodsFor: 'converting'!
  650. & aNumber
  651. <return self & aNumber>
  652. !
  653. @ aNumber
  654. ^Point x: self y: aNumber
  655. !
  656. asJSON
  657. ^self
  658. !
  659. asJavascript
  660. ^'(', self printString, ')'
  661. !
  662. asPoint
  663. ^Point x: self y: self
  664. !
  665. asString
  666. ^self printString
  667. !
  668. atRandom
  669. ^(Random new next * self) truncated + 1
  670. !
  671. rounded
  672. <return Math.round(self);>
  673. !
  674. to: aNumber
  675. | array first last count |
  676. first := self truncated.
  677. last := aNumber truncated + 1.
  678. count := 1.
  679. array := Array new.
  680. (last - first) timesRepeat: [
  681. array at: count put: first.
  682. count := count + 1.
  683. first := first + 1].
  684. ^array
  685. !
  686. to: stop by: step
  687. | array value pos |
  688. value := self.
  689. array := Array new.
  690. pos := 1.
  691. step = 0 ifTrue: [self error: 'step must be non-zero'].
  692. step < 0
  693. ifTrue: [[ value >= stop ] whileTrue: [
  694. array at: pos put: value.
  695. pos := pos + 1.
  696. value := value + step]]
  697. ifFalse: [[ value <= stop ] whileTrue: [
  698. array at: pos put: value.
  699. pos := pos + 1.
  700. value := value + step]].
  701. ^array
  702. !
  703. truncated
  704. <
  705. if(self >>= 0) {
  706. return Math.floor(self);
  707. } else {
  708. return Math.floor(self * (-1)) * (-1);
  709. };
  710. >
  711. !
  712. | aNumber
  713. <return self | aNumber>
  714. ! !
  715. !Number methodsFor: 'copying'!
  716. copy
  717. ^self
  718. !
  719. deepCopy
  720. ^self copy
  721. ! !
  722. !Number methodsFor: 'enumerating'!
  723. timesRepeat: aBlock
  724. | integer count |
  725. integer := self truncated.
  726. count := 1.
  727. [count > self] whileFalse: [
  728. aBlock value.
  729. count := count + 1]
  730. !
  731. to: stop by: step do: aBlock
  732. | value |
  733. value := self.
  734. step = 0 ifTrue: [self error: 'step must be non-zero'].
  735. step < 0
  736. ifTrue: [[ value >= stop ] whileTrue: [
  737. aBlock value: value.
  738. value := value + step]]
  739. ifFalse: [[ value <= stop ] whileTrue: [
  740. aBlock value: value.
  741. value := value + step]]
  742. !
  743. to: stop do: aBlock
  744. "Evaluate aBlock for each number from self to aNumber."
  745. | nextValue |
  746. nextValue := self.
  747. [nextValue <= stop]
  748. whileTrue:
  749. [aBlock value: nextValue.
  750. nextValue := nextValue + 1]
  751. ! !
  752. !Number methodsFor: 'printing'!
  753. printShowingDecimalPlaces: placesDesired
  754. <return self.toFixed(placesDesired)>
  755. !
  756. printString
  757. <return String(self)>
  758. ! !
  759. !Number methodsFor: 'testing'!
  760. even
  761. ^ 0 = (self \\ 2)
  762. !
  763. isNumber
  764. ^true
  765. !
  766. isZero
  767. ^self = 0
  768. !
  769. negative
  770. "Answer whether the receiver is mathematically negative."
  771. ^ self < 0
  772. !
  773. odd
  774. ^ self even not
  775. !
  776. positive
  777. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  778. ^ self >= 0
  779. ! !
  780. !Number class methodsFor: 'instance creation'!
  781. pi
  782. <return Math.PI>
  783. ! !
  784. Object subclass: #Organizer
  785. instanceVariableNames: ''
  786. package: 'Kernel-Objects'!
  787. !Organizer methodsFor: 'accessing'!
  788. addElement: anObject
  789. <self.elements.addElement(anObject)>
  790. !
  791. elements
  792. ^ (self basicAt: 'elements') copy
  793. !
  794. removeElement: anObject
  795. <self.elements.removeElement(anObject)>
  796. ! !
  797. Object subclass: #Package
  798. instanceVariableNames: 'commitPathJs commitPathSt'
  799. package: 'Kernel-Objects'!
  800. !Package commentStamp!
  801. A Package is similar to a "class category" typically found in other Smalltalks like Pharo or Squeak. Amber does not have class categories anymore, it had in the beginning but now each class in the system knows which package it belongs to.
  802. A Package has a name, an Array of "requires", a comment and a Dictionary with other optional key value attributes. A Package can also be queried for its classes, but it will then resort to a reverse scan of all classes to find them.
  803. Packages are manipulated through "Smalltalk current", like for example finding one based on a name:
  804. Smalltalk current packageAt: 'Kernel'
  805. ...but you can also use:
  806. Package named: 'Kernel'
  807. A Package differs slightly from a Monticello package which can span multiple class categories using a naming convention based on hyphenation. But just as in Monticello a Package supports "class extensions" so a Package
  808. can define behaviors in foreign classes using a naming convention for method categories where the category starts with an asterisk and then the name of the owning package follows. This can easily be seen in for example class
  809. String where the method category "*IDE" defines #inspectOn: which thus is a method belonging to the IDE package.
  810. You can fetch a package from the server:
  811. Package fetch: 'Additional-Examples'!
  812. !Package methodsFor: 'accessing'!
  813. commitPathJs
  814. ^ commitPathJs ifNil: [self class defaultCommitPathJs]
  815. !
  816. commitPathJs: aString
  817. commitPathJs := aString
  818. !
  819. commitPathSt
  820. ^ commitPathSt ifNil: [self class defaultCommitPathSt]
  821. !
  822. commitPathSt: aString
  823. commitPathSt := aString
  824. !
  825. dependencies
  826. ^self propertyAt: 'dependencies' ifAbsent: [#()]
  827. !
  828. dependencies: anArray
  829. ^self propertyAt: 'dependencies' put: anArray
  830. !
  831. name
  832. <return self.pkgName>
  833. !
  834. name: aString
  835. <self.pkgName = aString>
  836. !
  837. organization
  838. ^ self basicAt: 'organization'
  839. !
  840. properties
  841. ^Smalltalk current readJSObject: (self basicAt: 'properties')
  842. ! !
  843. !Package methodsFor: 'classes'!
  844. classes
  845. ^ self organization elements
  846. !
  847. setupClasses
  848. self classes
  849. do: [ :each | ClassBuilder new setupClass: each ];
  850. do: [ :each | each initialize ]
  851. !
  852. sortedClasses
  853. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  854. ^self class sortedClasses: self classes
  855. ! !
  856. !Package methodsFor: 'printing'!
  857. printString
  858. ^self name
  859. ! !
  860. !Package methodsFor: 'private'!
  861. jsProperties
  862. <return self.properties>
  863. !
  864. jsProperties: aJSObject
  865. <return self.properties = aJSObject>
  866. !
  867. propertiesAsJSON
  868. <return JSON.stringify(self.properties)>
  869. ! !
  870. !Package methodsFor: 'properties'!
  871. propertyAt: key
  872. <return self.properties[key]>
  873. !
  874. propertyAt: key ifAbsent: block
  875. ^(self propertyAt: key) ifNil: [block value]
  876. !
  877. propertyAt: key put: value
  878. <return self.properties[key] = value>
  879. ! !
  880. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  881. !Package class methodsFor: 'accessing'!
  882. named: aPackageName
  883. ^Smalltalk current packageAt: aPackageName
  884. !
  885. named: aPackageName ifAbsent: aBlock
  886. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  887. ! !
  888. !Package class methodsFor: 'commit paths'!
  889. defaultCommitPathJs
  890. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  891. !
  892. defaultCommitPathJs: aString
  893. defaultCommitPathJs := aString
  894. !
  895. defaultCommitPathSt
  896. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  897. !
  898. defaultCommitPathSt: aString
  899. defaultCommitPathSt := aString
  900. !
  901. resetCommitPaths
  902. defaultCommitPathJs := nil.
  903. defaultCommitPathSt := nil.
  904. ! !
  905. !Package class methodsFor: 'loading-storing'!
  906. fetch: aPackageName
  907. self fetch: aPackageName prefix: self defaultCommitPathJs, '/'
  908. !
  909. fetch: aPackageName prefix: aPrefix
  910. jQuery
  911. getScript: (aPrefix , aPackageName , '.js')
  912. onSuccess: [
  913. (Package named: aPackageName) setupClasses ]
  914. ! !
  915. !Package class methodsFor: 'sorting'!
  916. sortedClasses: classes
  917. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  918. | children others nodes expandedClasses |
  919. children := #().
  920. others := #().
  921. classes do: [:each |
  922. (classes includes: each superclass)
  923. ifFalse: [children add: each]
  924. ifTrue: [others add: each]].
  925. nodes := children collect: [:each |
  926. ClassSorterNode on: each classes: others level: 0].
  927. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  928. expandedClasses := Array new.
  929. nodes do: [:aNode |
  930. aNode traverseClassesWith: expandedClasses].
  931. ^expandedClasses
  932. ! !
  933. Object subclass: #Point
  934. instanceVariableNames: 'x y'
  935. package: 'Kernel-Objects'!
  936. !Point commentStamp!
  937. A `Point` represents an x-y pair of numbers usually designating a geometric coordinate.
  938. Points are traditionally created using the binary `#@` message to a number:
  939. 100@120
  940. Points can then be arithmetically manipulated:
  941. 100@100 + (10@10)
  942. ...or for example:
  943. (100@100) * 2
  944. **NOTE:** Creating a Point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  945. 100@ -100 "but 100@-100 would not parse"
  946. Amber does not have much behavior in this class out-of-the-box.!
  947. !Point methodsFor: 'accessing'!
  948. x
  949. ^x
  950. !
  951. x: aNumber
  952. x := aNumber
  953. !
  954. y
  955. ^y
  956. !
  957. y: aNumber
  958. y := aNumber
  959. ! !
  960. !Point methodsFor: 'arithmetic'!
  961. * aPoint
  962. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  963. !
  964. + aPoint
  965. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  966. !
  967. - aPoint
  968. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  969. !
  970. / aPoint
  971. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  972. !
  973. = aPoint
  974. ^aPoint class = self class and: [
  975. (aPoint x = self x) & (aPoint y = self y)]
  976. ! !
  977. !Point methodsFor: 'converting'!
  978. asPoint
  979. ^self
  980. ! !
  981. !Point methodsFor: 'printing'!
  982. printString
  983. "Print receiver in classic x@y notation."
  984. ^String streamContents: [:stream |
  985. stream nextPutAll: x printString, '@'.
  986. (y notNil and: [y negative])
  987. ifTrue: [
  988. "Avoid ambiguous @- construct"
  989. stream space].
  990. stream nextPutAll: y printString]
  991. ! !
  992. !Point methodsFor: 'transforming'!
  993. translateBy: delta
  994. "Answer a Point translated by delta (an instance of Point)."
  995. ^(delta x + x) @ (delta y + y)
  996. ! !
  997. !Point class methodsFor: 'instance creation'!
  998. x: aNumber y: anotherNumber
  999. ^self new
  1000. x: aNumber;
  1001. y: anotherNumber;
  1002. yourself
  1003. ! !
  1004. Object subclass: #Random
  1005. instanceVariableNames: ''
  1006. package: 'Kernel-Objects'!
  1007. !Random commentStamp!
  1008. `Random` is a random number generator and is implemented as a trivial wrapper around javascript `Math.random()` and is used like this:
  1009. Random new next
  1010. 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`
  1011. 10 atRandom
  1012. ...and if you want a random number in a specific interval this also works:
  1013. (3 to: 7) atRandom
  1014. ...but 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:
  1015. 5 atRandom + 2
  1016. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1017. #('a' 'b' 'c') atRandom
  1018. ...or perhaps a letter from a `String`:
  1019. 'abc' atRandom
  1020. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1021. !Random methodsFor: 'accessing'!
  1022. next
  1023. <return Math.random()>
  1024. !
  1025. next: anInteger
  1026. ^(1 to: anInteger) collect: [:each | self next]
  1027. ! !
  1028. Object subclass: #Smalltalk
  1029. instanceVariableNames: ''
  1030. package: 'Kernel-Objects'!
  1031. !Smalltalk commentStamp!
  1032. Smalltalk has only one instance, accessed with `Smalltalk current`.
  1033. It represents the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  1034. The `smalltalk` object holds all class and packages defined in the system.
  1035. ## Classes
  1036. Classes can be accessed using the following methods:
  1037. - `#classes` answers the full list of Smalltalk classes in the system
  1038. - `#at:` answers a specific class of `nil`
  1039. ## Packages
  1040. Packages can be accessed using the following methods:
  1041. - `#packages` answers the full list of packages
  1042. - `#packageAt:` answers a specific class of `nil`
  1043. __note:__ classes and packages are accessed using strings, not symbols
  1044. ## Parsing
  1045. The `#parse:` method is used to parse Smalltalk source code.
  1046. It requires the `Compiler` package and the `js/parser.js` parser file in order to work!
  1047. !Smalltalk methodsFor: 'accessing'!
  1048. at: aSymbol
  1049. <return self[aSymbol._asString()]>
  1050. !
  1051. parse: aString
  1052. | result |
  1053. self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
  1054. ^result
  1055. !
  1056. readJSObject: anObject
  1057. <return self.readJSObject(anObject)>
  1058. !
  1059. reservedWords
  1060. "JavaScript reserved words"
  1061. <return self.reservedWords>
  1062. ! !
  1063. !Smalltalk methodsFor: 'classes'!
  1064. classes
  1065. <return self.classes()>
  1066. !
  1067. deleteClass: aClass
  1068. "Deletes a class by deleting its binding only. Use #removeClass instead"
  1069. <self.removeClass(aClass)>
  1070. !
  1071. removeClass: aClass
  1072. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  1073. aClass methodDictionary values do: [:each |
  1074. aClass removeCompiledMethod: each].
  1075. aClass class methodDictionary values do: [:each |
  1076. aClass class removeCompiledMethod: each].
  1077. self deleteClass: aClass.
  1078. SystemAnnouncer current
  1079. announce: (ClassRemoved new
  1080. theClass: aClass;
  1081. yourself)
  1082. ! !
  1083. !Smalltalk methodsFor: 'error handling'!
  1084. parseError: anException parsing: aString
  1085. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  1086. ! !
  1087. !Smalltalk methodsFor: 'packages'!
  1088. createPackage: packageName
  1089. "Create and bind a new package with given name and return it."
  1090. <return smalltalk.addPackage(packageName)>
  1091. !
  1092. deletePackage: packageName
  1093. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  1094. To remove a package, use #removePackage instead."
  1095. <delete smalltalk.packages[packageName]>
  1096. !
  1097. packageAt: packageName
  1098. <return self.packages[packageName]>
  1099. !
  1100. packageAt: packageName ifAbsent: aBlock
  1101. ^(self packageAt: packageName) ifNil: aBlock
  1102. !
  1103. packages
  1104. "Return all Package instances in the system."
  1105. <return self.packages.all()>
  1106. !
  1107. pseudoVariableNames
  1108. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  1109. !
  1110. removePackage: packageName
  1111. "Removes a package and all its classes."
  1112. | pkg |
  1113. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1114. pkg classes do: [:each |
  1115. self removeClass: each].
  1116. self deletePackage: packageName
  1117. !
  1118. renamePackage: packageName to: newName
  1119. "Rename a package."
  1120. | pkg |
  1121. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1122. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  1123. (self basicAt: 'packages') at: newName put: pkg.
  1124. pkg name: newName.
  1125. self deletePackage: packageName.
  1126. ! !
  1127. !Smalltalk methodsFor: 'private'!
  1128. basicParse: aString
  1129. <return smalltalk.parser.parse(aString)>
  1130. !
  1131. createPackage: packageName properties: aDict
  1132. "Needed to import .st files: they begin with this call."
  1133. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  1134. ^ self createPackage: packageName
  1135. ! !
  1136. Smalltalk class instanceVariableNames: 'current'!
  1137. !Smalltalk class methodsFor: 'accessing'!
  1138. current
  1139. <return smalltalk>
  1140. ! !
  1141. Object subclass: #Timeout
  1142. instanceVariableNames: 'rawTimeout'
  1143. package: 'Kernel-Objects'!
  1144. !Timeout commentStamp!
  1145. I am wrapping the returns from set{Timeout,Interval}.
  1146. Number suffices in browsers, but node.js returns an object.!
  1147. !Timeout methodsFor: 'accessing'!
  1148. rawTimeout: anObject
  1149. rawTimeout := anObject
  1150. ! !
  1151. !Timeout methodsFor: 'timeout/interval'!
  1152. clearInterval
  1153. <
  1154. var interval = self["@rawTimeout"];
  1155. clearInterval(interval);
  1156. >
  1157. !
  1158. clearTimeout
  1159. <
  1160. var timeout = self["@rawTimeout"];
  1161. clearTimeout(timeout);
  1162. >
  1163. ! !
  1164. !Timeout class methodsFor: 'instance creation'!
  1165. on: anObject
  1166. ^self new rawTimeout: anObject; yourself
  1167. ! !
  1168. Object subclass: #UndefinedObject
  1169. instanceVariableNames: ''
  1170. package: 'Kernel-Objects'!
  1171. !UndefinedObject commentStamp!
  1172. UndefinedObject describes the behavior of its sole instance, `nil`. `nil` represents a prior value for variables that have not been initialized, or for results which are meaningless.
  1173. `nil` is the Smalltalk representation of the `undefined` JavaScript object.!
  1174. !UndefinedObject methodsFor: 'class creation'!
  1175. subclass: aString instanceVariableNames: anotherString
  1176. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1177. !
  1178. subclass: aString instanceVariableNames: aString2 category: aString3
  1179. "Kept for compatibility."
  1180. self deprecatedAPI.
  1181. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1182. !
  1183. subclass: aString instanceVariableNames: aString2 package: aString3
  1184. ^ClassBuilder new
  1185. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  1186. ! !
  1187. !UndefinedObject methodsFor: 'converting'!
  1188. asJSON
  1189. ^null
  1190. ! !
  1191. !UndefinedObject methodsFor: 'copying'!
  1192. deepCopy
  1193. ^self
  1194. !
  1195. shallowCopy
  1196. ^self
  1197. ! !
  1198. !UndefinedObject methodsFor: 'printing'!
  1199. printString
  1200. ^'nil'
  1201. ! !
  1202. !UndefinedObject methodsFor: 'testing'!
  1203. ifNil: aBlock
  1204. "inlined in the Compiler"
  1205. ^self ifNil: aBlock ifNotNil: []
  1206. !
  1207. ifNil: aBlock ifNotNil: anotherBlock
  1208. "inlined in the Compiler"
  1209. ^aBlock value
  1210. !
  1211. ifNotNil: aBlock
  1212. "inlined in the Compiler"
  1213. ^self
  1214. !
  1215. ifNotNil: aBlock ifNil: anotherBlock
  1216. "inlined in the Compiler"
  1217. ^anotherBlock value
  1218. !
  1219. isNil
  1220. ^true
  1221. !
  1222. notNil
  1223. ^false
  1224. ! !
  1225. !UndefinedObject class methodsFor: 'instance creation'!
  1226. new
  1227. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1228. ! !