1
0

Kernel-Objects.st 30 KB

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