1
0

Kernel-Objects.st 29 KB

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