Kernel-Objects.st 32 KB

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