Kernel-Objects.st 31 KB

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