Kernel-Objects.st 32 KB

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