Kernel-Objects.st 32 KB

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