Kernel-Objects.st 31 KB

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