Kernel-Objects.st 32 KB

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