1
0

Kernel-Objects.st 30 KB

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