Kernel-Objects.st 30 KB

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