Kernel-Objects.st 31 KB

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