Kernel-Objects.st 32 KB

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