Kernel-Objects.st 31 KB

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