1
0

Kernel-Objects.st 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  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. Organizer subclass: #ClassOrganizer
  816. instanceVariableNames: ''
  817. package: 'Kernel-Objects'!
  818. !ClassOrganizer methodsFor: 'accessing'!
  819. addElement: aString
  820. super addElement: aString.
  821. SystemAnnouncer current announce: (ProtocolAdded new
  822. protocol: aString;
  823. theClass: self theClass;
  824. yourself)
  825. !
  826. removeElement: aString
  827. super removeElement: aString.
  828. SystemAnnouncer current announce: (ProtocolRemoved new
  829. protocol: aString;
  830. theClass: self theClass;
  831. yourself)
  832. !
  833. theClass
  834. < return self.theClass >
  835. ! !
  836. Organizer subclass: #PackageOrganizer
  837. instanceVariableNames: ''
  838. package: 'Kernel-Objects'!
  839. Object subclass: #Package
  840. instanceVariableNames: 'commitPathJs commitPathSt'
  841. package: 'Kernel-Objects'!
  842. !Package commentStamp!
  843. 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.
  844. 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.
  845. Packages are manipulated through "Smalltalk current", like for example finding one based on a name:
  846. Smalltalk current packageAt: 'Kernel'
  847. ...but you can also use:
  848. Package named: 'Kernel'
  849. 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
  850. 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
  851. String where the method category "*IDE" defines #inspectOn: which thus is a method belonging to the IDE package.
  852. You can fetch a package from the server:
  853. Package fetch: 'Additional-Examples'!
  854. !Package methodsFor: 'accessing'!
  855. commitPathJs
  856. ^ commitPathJs ifNil: [self class defaultCommitPathJs]
  857. !
  858. commitPathJs: aString
  859. commitPathJs := aString
  860. !
  861. commitPathSt
  862. ^ commitPathSt ifNil: [self class defaultCommitPathSt]
  863. !
  864. commitPathSt: aString
  865. commitPathSt := aString
  866. !
  867. dependencies
  868. ^self propertyAt: 'dependencies' ifAbsent: [#()]
  869. !
  870. dependencies: anArray
  871. ^self propertyAt: 'dependencies' put: anArray
  872. !
  873. name
  874. <return self.pkgName>
  875. !
  876. name: aString
  877. <self.pkgName = aString>
  878. !
  879. organization
  880. ^ self basicAt: 'organization'
  881. !
  882. properties
  883. ^Smalltalk current readJSObject: (self basicAt: 'properties')
  884. ! !
  885. !Package methodsFor: 'classes'!
  886. classes
  887. ^ self organization elements
  888. !
  889. setupClasses
  890. self classes
  891. do: [ :each | ClassBuilder new setupClass: each ];
  892. do: [ :each | each initialize ]
  893. !
  894. sortedClasses
  895. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  896. ^self class sortedClasses: self classes
  897. ! !
  898. !Package methodsFor: 'printing'!
  899. printString
  900. ^self name
  901. ! !
  902. !Package methodsFor: 'private'!
  903. jsProperties
  904. <return self.properties>
  905. !
  906. jsProperties: aJSObject
  907. <return self.properties = aJSObject>
  908. !
  909. propertiesAsJSON
  910. <return JSON.stringify(self.properties)>
  911. ! !
  912. !Package methodsFor: 'properties'!
  913. propertyAt: key
  914. <return self.properties[key]>
  915. !
  916. propertyAt: key ifAbsent: block
  917. ^(self propertyAt: key) ifNil: [block value]
  918. !
  919. propertyAt: key put: value
  920. <return self.properties[key] = value>
  921. ! !
  922. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  923. !Package class methodsFor: 'accessing'!
  924. named: aPackageName
  925. ^Smalltalk current packageAt: aPackageName
  926. !
  927. named: aPackageName ifAbsent: aBlock
  928. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  929. ! !
  930. !Package class methodsFor: 'commit paths'!
  931. commitPathsFromLoader
  932. <
  933. var cp = typeof amber !!== 'undefined' && amber.commitPath;
  934. if (!!cp) return;
  935. if (cp.js) self._defaultCommitPathJs_(cp.js);
  936. if (cp.st) self._defaultCommitPathSt_(cp.st);
  937. >
  938. !
  939. defaultCommitPathJs
  940. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  941. !
  942. defaultCommitPathJs: aString
  943. defaultCommitPathJs := aString
  944. !
  945. defaultCommitPathSt
  946. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  947. !
  948. defaultCommitPathSt: aString
  949. defaultCommitPathSt := aString
  950. !
  951. resetCommitPaths
  952. defaultCommitPathJs := nil.
  953. defaultCommitPathSt := nil.
  954. ! !
  955. !Package class methodsFor: 'initialization'!
  956. initialize
  957. super initialize.
  958. self commitPathsFromLoader
  959. ! !
  960. !Package class methodsFor: 'loading-storing'!
  961. fetch: aPackageName
  962. self fetch: aPackageName prefix: self defaultCommitPathJs, '/'
  963. !
  964. fetch: aPackageName prefix: aPrefix
  965. jQuery
  966. getScript: (aPrefix , aPackageName , '.js')
  967. onSuccess: [
  968. (Package named: aPackageName) setupClasses ]
  969. ! !
  970. !Package class methodsFor: 'sorting'!
  971. sortedClasses: classes
  972. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  973. | children others nodes expandedClasses |
  974. children := #().
  975. others := #().
  976. classes do: [:each |
  977. (classes includes: each superclass)
  978. ifFalse: [children add: each]
  979. ifTrue: [others add: each]].
  980. nodes := children collect: [:each |
  981. ClassSorterNode on: each classes: others level: 0].
  982. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  983. expandedClasses := Array new.
  984. nodes do: [:aNode |
  985. aNode traverseClassesWith: expandedClasses].
  986. ^expandedClasses
  987. ! !
  988. Object subclass: #Point
  989. instanceVariableNames: 'x y'
  990. package: 'Kernel-Objects'!
  991. !Point commentStamp!
  992. A `Point` represents an x-y pair of numbers usually designating a geometric coordinate.
  993. Points are traditionally created using the binary `#@` message to a number:
  994. 100@120
  995. Points can then be arithmetically manipulated:
  996. 100@100 + (10@10)
  997. ...or for example:
  998. (100@100) * 2
  999. **NOTE:** Creating a Point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  1000. 100@ -100 "but 100@-100 would not parse"
  1001. Amber does not have much behavior in this class out-of-the-box.!
  1002. !Point methodsFor: 'accessing'!
  1003. x
  1004. ^x
  1005. !
  1006. x: aNumber
  1007. x := aNumber
  1008. !
  1009. y
  1010. ^y
  1011. !
  1012. y: aNumber
  1013. y := aNumber
  1014. ! !
  1015. !Point methodsFor: 'arithmetic'!
  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. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1027. !
  1028. = aPoint
  1029. ^aPoint class = self class and: [
  1030. (aPoint x = self x) & (aPoint y = self y)]
  1031. ! !
  1032. !Point methodsFor: 'converting'!
  1033. asPoint
  1034. ^self
  1035. ! !
  1036. !Point methodsFor: 'printing'!
  1037. printString
  1038. "Print receiver in classic x@y notation."
  1039. ^String streamContents: [:stream |
  1040. stream nextPutAll: x printString, '@'.
  1041. (y notNil and: [y negative])
  1042. ifTrue: [
  1043. "Avoid ambiguous @- construct"
  1044. stream space].
  1045. stream nextPutAll: y printString]
  1046. ! !
  1047. !Point methodsFor: 'transforming'!
  1048. translateBy: delta
  1049. "Answer a Point translated by delta (an instance of Point)."
  1050. ^(delta x + x) @ (delta y + y)
  1051. ! !
  1052. !Point class methodsFor: 'instance creation'!
  1053. x: aNumber y: anotherNumber
  1054. ^self new
  1055. x: aNumber;
  1056. y: anotherNumber;
  1057. yourself
  1058. ! !
  1059. Object subclass: #Random
  1060. instanceVariableNames: ''
  1061. package: 'Kernel-Objects'!
  1062. !Random commentStamp!
  1063. `Random` is a random number generator and is implemented as a trivial wrapper around javascript `Math.random()` and is used like this:
  1064. Random new next
  1065. 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`
  1066. 10 atRandom
  1067. ...and if you want a random number in a specific interval this also works:
  1068. (3 to: 7) atRandom
  1069. ...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:
  1070. 5 atRandom + 2
  1071. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1072. #('a' 'b' 'c') atRandom
  1073. ...or perhaps a letter from a `String`:
  1074. 'abc' atRandom
  1075. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1076. !Random methodsFor: 'accessing'!
  1077. next
  1078. <return Math.random()>
  1079. !
  1080. next: anInteger
  1081. ^(1 to: anInteger) collect: [:each | self next]
  1082. ! !
  1083. Object subclass: #Smalltalk
  1084. instanceVariableNames: ''
  1085. package: 'Kernel-Objects'!
  1086. !Smalltalk commentStamp!
  1087. Smalltalk has only one instance, accessed with `Smalltalk current`.
  1088. It represents the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  1089. The `smalltalk` object holds all class and packages defined in the system.
  1090. ## Classes
  1091. Classes can be accessed using the following methods:
  1092. - `#classes` answers the full list of Smalltalk classes in the system
  1093. - `#at:` answers a specific class of `nil`
  1094. ## Packages
  1095. Packages can be accessed using the following methods:
  1096. - `#packages` answers the full list of packages
  1097. - `#packageAt:` answers a specific class of `nil`
  1098. __note:__ classes and packages are accessed using strings, not symbols
  1099. ## Parsing
  1100. The `#parse:` method is used to parse Smalltalk source code.
  1101. It requires the `Compiler` package and the `js/parser.js` parser file in order to work!
  1102. !Smalltalk methodsFor: 'accessing'!
  1103. at: aSymbol
  1104. <return self[aSymbol._asString()]>
  1105. !
  1106. parse: aString
  1107. | result |
  1108. self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
  1109. ^result
  1110. !
  1111. readJSObject: anObject
  1112. <return self.readJSObject(anObject)>
  1113. !
  1114. reservedWords
  1115. "JavaScript reserved words"
  1116. <return self.reservedWords>
  1117. !
  1118. version
  1119. "Answer the version string of Amber"
  1120. ^ '0.10'
  1121. ! !
  1122. !Smalltalk methodsFor: 'classes'!
  1123. classes
  1124. <return self.classes()>
  1125. !
  1126. deleteClass: aClass
  1127. "Deletes a class by deleting its binding only. Use #removeClass instead"
  1128. <self.removeClass(aClass)>
  1129. !
  1130. removeClass: aClass
  1131. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  1132. self deleteClass: aClass.
  1133. SystemAnnouncer current
  1134. announce: (ClassRemoved new
  1135. theClass: aClass;
  1136. yourself)
  1137. ! !
  1138. !Smalltalk methodsFor: 'error handling'!
  1139. asSmalltalkException: anObject
  1140. "A JavaScript exception may be thrown.
  1141. We then need to convert it back to a Smalltalk object"
  1142. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  1143. ifTrue: [ anObject ]
  1144. ifFalse: [ JavaScriptException on: anObject ]
  1145. !
  1146. parseError: anException parsing: aString
  1147. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  1148. ! !
  1149. !Smalltalk methodsFor: 'packages'!
  1150. createPackage: packageName
  1151. "Create and bind a new package with given name and return it."
  1152. <return smalltalk.addPackage(packageName)>
  1153. !
  1154. deletePackage: packageName
  1155. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  1156. To remove a package, use #removePackage instead."
  1157. <delete smalltalk.packages[packageName]>
  1158. !
  1159. packageAt: packageName
  1160. <return self.packages[packageName]>
  1161. !
  1162. packageAt: packageName ifAbsent: aBlock
  1163. ^(self packageAt: packageName) ifNil: aBlock
  1164. !
  1165. packages
  1166. "Return all Package instances in the system."
  1167. <return self.packages.all()>
  1168. !
  1169. pseudoVariableNames
  1170. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  1171. !
  1172. removePackage: packageName
  1173. "Removes a package and all its classes."
  1174. | pkg |
  1175. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1176. pkg classes do: [:each |
  1177. self removeClass: each].
  1178. self deletePackage: packageName
  1179. !
  1180. renamePackage: packageName to: newName
  1181. "Rename a package."
  1182. | pkg |
  1183. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1184. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  1185. (self basicAt: 'packages') at: newName put: pkg.
  1186. pkg name: newName.
  1187. self deletePackage: packageName.
  1188. ! !
  1189. !Smalltalk methodsFor: 'private'!
  1190. basicParse: aString
  1191. <return smalltalk.parser.parse(aString)>
  1192. !
  1193. createPackage: packageName properties: aDict
  1194. "Needed to import .st files: they begin with this call."
  1195. self deprecatedAPI.
  1196. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  1197. ^ self createPackage: packageName
  1198. ! !
  1199. !Smalltalk methodsFor: 'testing'!
  1200. isSmalltalkObject: anObject
  1201. "Consider anObject a Smalltalk object if it has a 'klass' property.
  1202. Note that this may be unaccurate"
  1203. <return typeof anObject.klass !!== 'undefined'>
  1204. ! !
  1205. Smalltalk class instanceVariableNames: 'current'!
  1206. !Smalltalk class methodsFor: 'accessing'!
  1207. current
  1208. <return smalltalk>
  1209. ! !
  1210. Object subclass: #Timeout
  1211. instanceVariableNames: 'rawTimeout'
  1212. package: 'Kernel-Objects'!
  1213. !Timeout commentStamp!
  1214. I am wrapping the returns from set{Timeout,Interval}.
  1215. Number suffices in browsers, but node.js returns an object.!
  1216. !Timeout methodsFor: 'accessing'!
  1217. rawTimeout: anObject
  1218. rawTimeout := anObject
  1219. ! !
  1220. !Timeout methodsFor: 'timeout/interval'!
  1221. clearInterval
  1222. <
  1223. var interval = self["@rawTimeout"];
  1224. clearInterval(interval);
  1225. >
  1226. !
  1227. clearTimeout
  1228. <
  1229. var timeout = self["@rawTimeout"];
  1230. clearTimeout(timeout);
  1231. >
  1232. ! !
  1233. !Timeout class methodsFor: 'instance creation'!
  1234. on: anObject
  1235. ^self new rawTimeout: anObject; yourself
  1236. ! !
  1237. Object subclass: #UndefinedObject
  1238. instanceVariableNames: ''
  1239. package: 'Kernel-Objects'!
  1240. !UndefinedObject commentStamp!
  1241. 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.
  1242. `nil` is the Smalltalk representation of the `undefined` JavaScript object.!
  1243. !UndefinedObject methodsFor: 'class creation'!
  1244. subclass: aString instanceVariableNames: anotherString
  1245. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1246. !
  1247. subclass: aString instanceVariableNames: aString2 category: aString3
  1248. "Kept for compatibility."
  1249. self deprecatedAPI.
  1250. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1251. !
  1252. subclass: aString instanceVariableNames: aString2 package: aString3
  1253. ^ClassBuilder new
  1254. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  1255. ! !
  1256. !UndefinedObject methodsFor: 'converting'!
  1257. asJSON
  1258. ^null
  1259. ! !
  1260. !UndefinedObject methodsFor: 'copying'!
  1261. deepCopy
  1262. ^self
  1263. !
  1264. shallowCopy
  1265. ^self
  1266. ! !
  1267. !UndefinedObject methodsFor: 'printing'!
  1268. printString
  1269. ^'nil'
  1270. ! !
  1271. !UndefinedObject methodsFor: 'testing'!
  1272. ifNil: aBlock
  1273. "inlined in the Compiler"
  1274. ^self ifNil: aBlock ifNotNil: []
  1275. !
  1276. ifNil: aBlock ifNotNil: anotherBlock
  1277. "inlined in the Compiler"
  1278. ^aBlock value
  1279. !
  1280. ifNotNil: aBlock
  1281. "inlined in the Compiler"
  1282. ^self
  1283. !
  1284. ifNotNil: aBlock ifNil: anotherBlock
  1285. "inlined in the Compiler"
  1286. ^anotherBlock value
  1287. !
  1288. isNil
  1289. ^true
  1290. !
  1291. notNil
  1292. ^false
  1293. ! !
  1294. !UndefinedObject class methodsFor: 'instance creation'!
  1295. new
  1296. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1297. ! !