Kernel-Objects.st 32 KB

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