Kernel-Objects.st 30 KB

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