Kernel-Objects.st 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. Smalltalk current createPackage: 'Kernel-Objects'!
  2. nil subclass: #Object
  3. instanceVariableNames: ''
  4. package: 'Kernel-Objects'!
  5. !Object commentStamp!
  6. **I am the root of the Smalltalk class system**. All classes in the system are subclasses of me.
  7. I provide default behavior common to all normal objects, such as:
  8. - accessing
  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. I have no instance variable.
  16. ##Access
  17. Instance variables can be accessed with `#instVarAt:` and `#instVarAt:put:`. `#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. I 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: aString
  52. < return self['@'+aString] >
  53. !
  54. instVarAt: aString put: anObject
  55. < self['@' + aString] = anObject >
  56. !
  57. size
  58. self error: 'Object not indexable'
  59. !
  60. value
  61. <return self.valueOf()>
  62. !
  63. yourself
  64. ^self
  65. ! !
  66. !Object methodsFor: 'comparing'!
  67. = anObject
  68. ^self == anObject
  69. !
  70. == anObject
  71. ^self identityHash = anObject identityHash
  72. !
  73. ~= anObject
  74. ^(self = anObject) = false
  75. !
  76. ~~ anObject
  77. ^(self == anObject) = false
  78. ! !
  79. !Object methodsFor: 'converting'!
  80. -> anObject
  81. ^Association key: self value: anObject
  82. !
  83. asJSON
  84. | variables |
  85. variables := HashedCollection new.
  86. self class allInstanceVariableNames do: [:each |
  87. variables at: each put: (self instVarAt: each) asJSON].
  88. ^variables
  89. !
  90. asJSONString
  91. ^JSON stringify: self asJSON
  92. !
  93. asJavascript
  94. ^self asString
  95. !
  96. asString
  97. ^self printString
  98. !
  99. test
  100. | a |
  101. a := 1.
  102. self halt
  103. ! !
  104. !Object methodsFor: 'copying'!
  105. copy
  106. ^self shallowCopy postCopy
  107. !
  108. deepCopy
  109. <
  110. var copy = self.klass._new();
  111. for(var i in self) {
  112. if(/^@.+/.test(i)) {
  113. copy[i] = self[i]._deepCopy();
  114. }
  115. }
  116. return copy;
  117. >
  118. !
  119. postCopy
  120. !
  121. shallowCopy
  122. <
  123. var copy = self.klass._new();
  124. for(var i in self) {
  125. if(/^@.+/.test(i)) {
  126. copy[i] = self[i];
  127. }
  128. }
  129. return copy;
  130. >
  131. ! !
  132. !Object methodsFor: 'error handling'!
  133. deprecatedAPI
  134. "Just a simple way to deprecate methods.
  135. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  136. but it could in the future."
  137. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'
  138. !
  139. doesNotUnderstand: aMessage
  140. MessageNotUnderstood new
  141. receiver: self;
  142. message: aMessage;
  143. signal
  144. !
  145. error: aString
  146. Error signal: aString
  147. !
  148. halt
  149. self error: 'Halt encountered'
  150. !
  151. shouldNotImplement
  152. self error: 'This method should not be implemented in ', self class name
  153. !
  154. subclassResponsibility
  155. self error: 'This method is a responsibility of a subclass'
  156. !
  157. throw: anObject
  158. < throw anObject >
  159. !
  160. try: aBlock catch: anotherBlock
  161. <try{return aBlock._value()} catch(e) {return anotherBlock._value_(e)}>
  162. ! !
  163. !Object methodsFor: 'initialization'!
  164. initialize
  165. ! !
  166. !Object methodsFor: 'inspecting'!
  167. inspect
  168. InspectorHandler inspect: self
  169. !
  170. inspectOn: anInspector
  171. | variables |
  172. variables := Dictionary new.
  173. variables at: '#self' put: self.
  174. self class allInstanceVariableNames do: [:each |
  175. variables at: each put: (self instVarAt: each)].
  176. anInspector
  177. setLabel: self printString;
  178. setVariables: variables
  179. ! !
  180. !Object methodsFor: 'message handling'!
  181. basicPerform: aString
  182. ^self basicPerform: aString withArguments: #()
  183. !
  184. basicPerform: aString withArguments: aCollection
  185. <return self[aString].apply(self, aCollection);>
  186. !
  187. perform: aString
  188. ^self perform: aString withArguments: #()
  189. !
  190. perform: aString withArguments: aCollection
  191. <return smalltalk.send(self, aString._asSelector(), aCollection)>
  192. ! !
  193. !Object methodsFor: 'printing'!
  194. printOn: aStream
  195. "Append to the aStream, a string representing the receiver."
  196. aStream nextPutAll: (self class name first isVowel
  197. ifTrue: [ 'an ' ]
  198. ifFalse: [ 'a ' ]).
  199. aStream nextPutAll: self class name
  200. !
  201. printString
  202. "Answer a String representation of the receiver."
  203. ^ String streamContents: [ :stream | self printOn: stream ]
  204. ! !
  205. !Object methodsFor: 'streaming'!
  206. putOn: aStream
  207. aStream nextPut: self
  208. ! !
  209. !Object methodsFor: 'testing'!
  210. ifNil: aBlock
  211. "inlined in the Compiler"
  212. ^self
  213. !
  214. ifNil: aBlock ifNotNil: anotherBlock
  215. "inlined in the Compiler"
  216. ^anotherBlock value: self
  217. !
  218. ifNotNil: aBlock
  219. "inlined in the Compiler"
  220. ^aBlock value: self
  221. !
  222. ifNotNil: aBlock ifNil: anotherBlock
  223. "inlined in the Compiler"
  224. ^aBlock value: self
  225. !
  226. isBehavior
  227. ^ false
  228. !
  229. isBoolean
  230. ^ false
  231. !
  232. isClass
  233. ^false
  234. !
  235. isCompiledMethod
  236. ^ false
  237. !
  238. isImmutable
  239. ^ false
  240. !
  241. isKindOf: aClass
  242. ^(self isMemberOf: aClass)
  243. ifTrue: [true]
  244. ifFalse: [self class inheritsFrom: aClass]
  245. !
  246. isMemberOf: aClass
  247. ^self class = aClass
  248. !
  249. isMetaclass
  250. ^false
  251. !
  252. isNil
  253. ^false
  254. !
  255. isNumber
  256. ^false
  257. !
  258. isPackage
  259. ^ false
  260. !
  261. isParseFailure
  262. ^false
  263. !
  264. isString
  265. ^false
  266. !
  267. isSymbol
  268. ^false
  269. !
  270. notNil
  271. ^self isNil not
  272. !
  273. respondsTo: aSelector
  274. ^self class canUnderstand: aSelector
  275. ! !
  276. !Object class methodsFor: 'helios'!
  277. heliosClass
  278. "Should be an Helios extension. Unfortunately, since helios can browse remote
  279. environments, we can't extend base classes"
  280. ^ 'class'
  281. ! !
  282. !Object class methodsFor: 'initialization'!
  283. initialize
  284. "no op"
  285. ! !
  286. Object subclass: #Boolean
  287. instanceVariableNames: ''
  288. package: 'Kernel-Objects'!
  289. !Boolean commentStamp!
  290. I define the protocol for logic testing operations and conditional control structures for the logical values (see the `controlling` protocol).
  291. I have two instances, `true` and `false`.
  292. I am directly mapped to JavaScript Boolean. The `true` and `false` objects are the JavaScript boolean objects.
  293. ## Usage Example:
  294. aBoolean not ifTrue: [ ... ] ifFalse: [ ... ]!
  295. !Boolean methodsFor: 'comparing'!
  296. = aBoolean
  297. <
  298. if(!! aBoolean._isBoolean || !! aBoolean._isBoolean()) {
  299. return false;
  300. }
  301. return Boolean(self == true) == aBoolean
  302. >
  303. !
  304. == aBoolean
  305. ^self = aBoolean
  306. ! !
  307. !Boolean methodsFor: 'controlling'!
  308. & aBoolean
  309. <
  310. if(self == true) {
  311. return aBoolean;
  312. } else {
  313. return false;
  314. }
  315. >
  316. !
  317. and: aBlock
  318. ^self = true
  319. ifTrue: aBlock
  320. ifFalse: [false]
  321. !
  322. ifFalse: aBlock
  323. "inlined in the Compiler"
  324. ^self ifTrue: [] ifFalse: aBlock
  325. !
  326. ifFalse: aBlock ifTrue: anotherBlock
  327. "inlined in the Compiler"
  328. ^self ifTrue: anotherBlock ifFalse: aBlock
  329. !
  330. ifTrue: aBlock
  331. "inlined in the Compiler"
  332. ^self ifTrue: aBlock ifFalse: []
  333. !
  334. ifTrue: aBlock ifFalse: anotherBlock
  335. "inlined in the Compiler"
  336. <
  337. if(self == true) {
  338. return aBlock._value();
  339. } else {
  340. return anotherBlock._value();
  341. }
  342. >
  343. !
  344. not
  345. ^self = false
  346. !
  347. or: aBlock
  348. ^self = true
  349. ifTrue: [true]
  350. ifFalse: aBlock
  351. !
  352. | aBoolean
  353. <
  354. if(self == true) {
  355. return true;
  356. } else {
  357. return aBoolean;
  358. }
  359. >
  360. ! !
  361. !Boolean methodsFor: 'converting'!
  362. asBit
  363. ^ self ifTrue: [ 1 ] ifFalse: [ 0 ]
  364. !
  365. asJSON
  366. ^self
  367. !
  368. asString
  369. < return self.toString() >
  370. ! !
  371. !Boolean methodsFor: 'copying'!
  372. deepCopy
  373. ^self
  374. !
  375. shallowCopy
  376. ^self
  377. ! !
  378. !Boolean methodsFor: 'printing'!
  379. printOn: aStream
  380. aStream nextPutAll: self asString
  381. ! !
  382. !Boolean methodsFor: 'testing'!
  383. isBoolean
  384. ^ true
  385. !
  386. isImmutable
  387. ^ true
  388. ! !
  389. Object subclass: #Date
  390. instanceVariableNames: ''
  391. package: 'Kernel-Objects'!
  392. !Date commentStamp!
  393. I am used to work with both dates and times. Therefore `Date today` and `Date now` are both valid in
  394. Amber and answer the same date object.
  395. Date directly maps to the `Date()` JavaScript constructor, and Amber date objects are JavaScript date objects.
  396. ## API
  397. The class-side `instance creation` protocol contains some convenience methods for creating date/time objects such as `#fromSeconds:`.
  398. Arithmetic and comparison is supported (see the `comparing` and `arithmetic` protocols).
  399. The `converting` protocol provides convenience methods for various convertions (to numbers, strings, etc.).!
  400. !Date methodsFor: 'accessing'!
  401. day
  402. ^self dayOfWeek
  403. !
  404. day: aNumber
  405. self dayOfWeek: aNumber
  406. !
  407. dayOfMonth
  408. <return self.getDate()>
  409. !
  410. dayOfMonth: aNumber
  411. <self.setDate(aNumber)>
  412. !
  413. dayOfWeek
  414. <return self.getDay() + 1>
  415. !
  416. dayOfWeek: aNumber
  417. <return self.setDay(aNumber - 1)>
  418. !
  419. hours
  420. <return self.getHours()>
  421. !
  422. hours: aNumber
  423. <self.setHours(aNumber)>
  424. !
  425. milliseconds
  426. <return self.getMilliseconds()>
  427. !
  428. milliseconds: aNumber
  429. <self.setMilliseconds(aNumber)>
  430. !
  431. minutes
  432. <return self.getMinutes()>
  433. !
  434. minutes: aNumber
  435. <self.setMinutes(aNumber)>
  436. !
  437. month
  438. <return self.getMonth() + 1>
  439. !
  440. month: aNumber
  441. <self.setMonth(aNumber - 1)>
  442. !
  443. seconds
  444. <return self.getSeconds()>
  445. !
  446. seconds: aNumber
  447. <self.setSeconds(aNumber)>
  448. !
  449. time
  450. <return self.getTime()>
  451. !
  452. time: aNumber
  453. <self.setTime(aNumber)>
  454. !
  455. year
  456. <return self.getFullYear()>
  457. !
  458. year: aNumber
  459. <self.setFullYear(aNumber)>
  460. ! !
  461. !Date methodsFor: 'arithmetic'!
  462. + aDate
  463. <return self + aDate>
  464. !
  465. - aDate
  466. <return self - aDate>
  467. ! !
  468. !Date methodsFor: 'comparing'!
  469. < aDate
  470. <return self < aDate>
  471. !
  472. <= aDate
  473. <return self <= aDate>
  474. !
  475. > aDate
  476. <return self >> aDate>
  477. !
  478. >= aDate
  479. <return self >>= aDate>
  480. ! !
  481. !Date methodsFor: 'converting'!
  482. asDateString
  483. <return self.toDateString()>
  484. !
  485. asLocaleString
  486. <return self.toLocaleString()>
  487. !
  488. asMilliseconds
  489. ^self time
  490. !
  491. asNumber
  492. ^self asMilliseconds
  493. !
  494. asString
  495. <return self.toString()>
  496. !
  497. asTimeString
  498. <return self.toTimeString()>
  499. ! !
  500. !Date methodsFor: 'printing'!
  501. printOn: aStream
  502. aStream nextPutAll: self asString
  503. ! !
  504. !Date class methodsFor: 'helios'!
  505. heliosClass
  506. ^ 'magnitude'
  507. ! !
  508. !Date class methodsFor: 'instance creation'!
  509. fromMilliseconds: aNumber
  510. ^self new: aNumber
  511. !
  512. fromSeconds: aNumber
  513. ^self fromMilliseconds: aNumber * 1000
  514. !
  515. fromString: aString
  516. "Example: Date fromString('2011/04/15 00:00:00')"
  517. ^self new: aString
  518. !
  519. millisecondsToRun: aBlock
  520. | t |
  521. t := Date now.
  522. aBlock value.
  523. ^Date now - t
  524. !
  525. new: anObject
  526. <return new Date(anObject)>
  527. !
  528. now
  529. ^self today
  530. !
  531. today
  532. ^self new
  533. ! !
  534. Object subclass: #InspectorHandler
  535. instanceVariableNames: ''
  536. package: 'Kernel-Objects'!
  537. !InspectorHandler commentStamp!
  538. I am responsible for inspecting object.
  539. My class-side `inspector` inst var holds the current inspector I'm delegating object inspection to.
  540. The default inspector object is the transcript.!
  541. InspectorHandler class instanceVariableNames: 'inspector'!
  542. !InspectorHandler class methodsFor: 'accessing'!
  543. inspector
  544. ^ inspector ifNil: [ inspector := Transcript ]
  545. ! !
  546. !InspectorHandler class methodsFor: 'registration'!
  547. inspect: anObject
  548. ^ self inspector inspect: anObject
  549. !
  550. register: anInspector
  551. inspector := anInspector
  552. ! !
  553. Object subclass: #InterfacingObject
  554. instanceVariableNames: ''
  555. package: 'Kernel-Objects'!
  556. !InterfacingObject commentStamp!
  557. I am superclass of all object that interface with user or environment. `Widget` and a few other classes are subclasses of me. I delegate all of the above APIs to `PlatformInterface`.
  558. ## API
  559. self alert: 'Hey, there is a problem'.
  560. self confirm: 'Affirmative?'.
  561. self prompt: 'Your name:'.
  562. self ajax: #{
  563. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  564. }.!
  565. !InterfacingObject methodsFor: 'actions'!
  566. ajax: anObject
  567. ^PlatformInterface ajax: anObject
  568. !
  569. alert: aString
  570. ^PlatformInterface alert: aString
  571. !
  572. confirm: aString
  573. ^PlatformInterface confirm: aString
  574. !
  575. prompt: aString
  576. ^PlatformInterface prompt: aString
  577. ! !
  578. InterfacingObject subclass: #Environment
  579. instanceVariableNames: ''
  580. package: 'Kernel-Objects'!
  581. !Environment commentStamp!
  582. I provide an unified entry point to manipulate Amber packages, classes and methods.
  583. Typical use cases include IDEs, remote access and restricting browsing.!
  584. !Environment methodsFor: 'accessing'!
  585. allSelectors
  586. ^ (Smalltalk current at: 'allSelectors') value
  587. !
  588. availableClassNames
  589. ^ Smalltalk current classes
  590. collect: [ :each | each name ]
  591. !
  592. availablePackageNames
  593. ^ Smalltalk current packages
  594. collect: [ :each | each name ]
  595. !
  596. availableProtocolsFor: aClass
  597. | protocols |
  598. protocols := aClass protocols.
  599. aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
  600. ^ protocols asSet asArray
  601. !
  602. classBuilder
  603. ^ ClassBuilder new
  604. !
  605. classNamed: aString
  606. ^ (Smalltalk current at: aString asSymbol)
  607. ifNil: [ self error: 'Invalid class name' ]
  608. !
  609. classes
  610. ^ Smalltalk current classes
  611. !
  612. packages
  613. ^ Smalltalk current packages
  614. !
  615. systemAnnouncer
  616. ^ (Smalltalk current at: #SystemAnnouncer) current
  617. ! !
  618. !Environment methodsFor: 'actions'!
  619. commitPackage: aPackage
  620. aPackage commit
  621. !
  622. copyClass: aClass to: aClassName
  623. (Smalltalk current at: aClassName)
  624. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  625. ClassBuilder new copyClass: aClass named: aClassName
  626. !
  627. eval: aString on: aReceiver
  628. | compiler |
  629. compiler := Compiler new.
  630. [ compiler parseExpression: aString ] on: Error do: [ :ex |
  631. ^ self alert: ex messageText ].
  632. ^ compiler evaluateExpression: aString on: aReceiver
  633. !
  634. inspect: anObject
  635. InspectorHandler inspector inspect: anObject
  636. !
  637. moveClass: aClass toPackage: aPackageName
  638. | package |
  639. package := Package named: aPackageName.
  640. package ifNil: [ self error: 'Invalid package name' ].
  641. package == aClass package ifTrue: [ ^ self ].
  642. aClass package: package
  643. !
  644. moveMethod: aMethod toClass: aClassName
  645. | destinationClass |
  646. destinationClass := Smalltalk current at: aClassName asSymbol.
  647. destinationClass ifNil: [ self error: 'Invalid class name' ].
  648. destinationClass == aMethod methodClass ifTrue: [ ^ self ].
  649. destinationClass
  650. compile: aMethod source
  651. category: aMethod protocol.
  652. aMethod methodClass
  653. removeCompiledMethod: aMethod
  654. !
  655. moveMethod: aMethod toProtocol: aProtocol
  656. aMethod category: aProtocol
  657. !
  658. registerErrorHandler: anErrorHandler
  659. ErrorHandler setCurrent: anErrorHandler
  660. !
  661. registerInspector: anInspector
  662. InspectorHandler register: anInspector
  663. !
  664. registerProgressHandler: aProgressHandler
  665. ProgressHandler setCurrent: aProgressHandler
  666. !
  667. removeClass: aClass
  668. Smalltalk current removeClass: aClass
  669. !
  670. removeMethod: aMethod
  671. aMethod methodClass removeCompiledMethod: aMethod
  672. !
  673. removeProtocol: aString from: aClass
  674. (aClass methods
  675. select: [ :each | each protocol = aString ])
  676. do: [ :each | aClass removeCompiledMethod: each ]
  677. !
  678. renameClass: aClass to: aClassName
  679. (Smalltalk current at: aClassName)
  680. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  681. ClassBuilder new renameClass: aClass to: aClassName
  682. !
  683. renameProtocol: aString to: anotherString in: aClass
  684. (aClass methods
  685. select: [ :each | each protocol = aString ])
  686. do: [ :each | each protocol: anotherString ]
  687. !
  688. setClassCommentOf: aClass to: aString
  689. aClass comment: aString
  690. ! !
  691. !Environment methodsFor: 'compiling'!
  692. addInstVarNamed: aString to: aClass
  693. self classBuilder
  694. addSubclassOf: aClass superclass
  695. named: aClass name
  696. instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
  697. package: aClass package name
  698. !
  699. compileClassComment: aString for: aClass
  700. aClass comment: aString
  701. !
  702. compileClassDefinition: aString
  703. self eval: aString on: DoIt new
  704. !
  705. compileMethod: sourceCode for: class protocol: protocol
  706. ^ class
  707. compile: sourceCode
  708. category: protocol
  709. ! !
  710. !Environment methodsFor: 'error handling'!
  711. evaluate: aBlock on: anErrorClass do: exceptionBlock
  712. "Evaluate a block and catch exceptions happening on the environment stack"
  713. self try: aBlock catch: [ :exception |
  714. (exception isKindOf: (self classNamed: anErrorClass name))
  715. ifTrue: [ exceptionBlock value: exception ]
  716. ifFalse: [ exception signal ] ]
  717. ! !
  718. Object subclass: #JSObjectProxy
  719. instanceVariableNames: 'jsObject'
  720. package: 'Kernel-Objects'!
  721. !JSObjectProxy commentStamp!
  722. I handle sending messages to JavaScript objects, making JavaScript object accessing from Amber fully transparent.
  723. My instances make intensive use of `#doesNotUnderstand:`.
  724. My instances are automatically created by Amber whenever a message is sent to a JavaScript object.
  725. ## Usage examples
  726. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  727. window alert: 'hello world'.
  728. window inspect.
  729. (window jQuery: 'body') append: 'hello world'
  730. Amber 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.
  731. ## Message conversion rules
  732. - `someUser name` becomes `someUser.name`
  733. - `someUser name: 'John'` becomes `someUser name = "John"`
  734. - `console log: 'hello world'` becomes `console.log('hello world')`
  735. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  736. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  737. !JSObjectProxy methodsFor: 'accessing'!
  738. at: aString
  739. <return self['@jsObject'][aString]>
  740. !
  741. at: aString ifAbsent: aBlock
  742. "return the aString property or evaluate aBlock if the property is not defined on the object"
  743. <
  744. var obj = self['@jsObject'];
  745. return aString in obj ? obj[aString] : aBlock._value();
  746. >
  747. !
  748. at: aString ifPresent: aBlock
  749. "return the evaluation of aBlock with the value if the property is defined or return nil"
  750. <
  751. var obj = self['@jsObject'];
  752. return aString in obj ? aBlock._value_(obj[aString]) : nil;
  753. >
  754. !
  755. at: aString ifPresent: aBlock ifAbsent: anotherBlock
  756. "return the evaluation of aBlock with the value if the property is defined
  757. or return value of anotherBlock"
  758. <
  759. var obj = self['@jsObject'];
  760. return aString in obj ? aBlock._value_(obj[aString]) : anotherBlock._value();
  761. >
  762. !
  763. at: aString put: anObject
  764. <self['@jsObject'][aString] = anObject>
  765. !
  766. jsObject
  767. ^jsObject
  768. !
  769. jsObject: aJSObject
  770. jsObject := aJSObject
  771. !
  772. lookupProperty: aString
  773. "Looks up a property in JS object.
  774. Answer the property if it is present, or nil if it is not present."
  775. <return aString in self._jsObject() ? aString : nil>
  776. !
  777. value
  778. "if attribute 'value' exists on the JS object return it,
  779. otherwise return the result of Object>>value."
  780. ^ self
  781. at: 'value'
  782. ifAbsent: [ super value ]
  783. ! !
  784. !JSObjectProxy methodsFor: 'enumerating'!
  785. keysAndValuesDo: aBlock
  786. <
  787. var o = self['@jsObject'];
  788. for(var i in o) {
  789. aBlock._value_value_(i, o[i]);
  790. }
  791. >
  792. ! !
  793. !JSObjectProxy methodsFor: 'printing'!
  794. printOn: aStream
  795. aStream nextPutAll: self jsObject toString
  796. ! !
  797. !JSObjectProxy methodsFor: 'proxy'!
  798. addObjectVariablesTo: aDictionary
  799. <
  800. for(var i in self['@jsObject']) {
  801. aDictionary._at_put_(i, self['@jsObject'][i]);
  802. }
  803. >
  804. !
  805. doesNotUnderstand: aMessage
  806. ^ (self lookupProperty: aMessage selector asJavaScriptSelector)
  807. ifNil: [ super doesNotUnderstand: aMessage ]
  808. ifNotNil: [ :jsSelector |
  809. self
  810. forwardMessage: jsSelector
  811. withArguments: aMessage arguments ]
  812. !
  813. forwardMessage: aString withArguments: anArray
  814. <
  815. return smalltalk.send(self._jsObject(), aString, anArray);
  816. >
  817. !
  818. inspectOn: anInspector
  819. | variables |
  820. variables := Dictionary new.
  821. variables at: '#self' put: self jsObject.
  822. anInspector setLabel: self printString.
  823. self addObjectVariablesTo: variables.
  824. anInspector setVariables: variables
  825. ! !
  826. !JSObjectProxy class methodsFor: 'instance creation'!
  827. on: aJSObject
  828. ^self new
  829. jsObject: aJSObject;
  830. yourself
  831. ! !
  832. Object subclass: #Number
  833. instanceVariableNames: ''
  834. package: 'Kernel-Objects'!
  835. !Number commentStamp!
  836. I am the Amber representation for all numbers.
  837. I am directly mapped to JavaScript Number.
  838. ## API
  839. I provide all necessary methods for arithmetic operations, comparison, conversion and so on with numbers.
  840. My instances can also be used to evaluate a block a fixed number of times:
  841. 5 timesRepeat: [Transcript show: 'This will be printed 5 times'; cr].
  842. 1 to: 5 do: [:aNumber| Transcript show: aNumber asString; cr].
  843. 1 to: 10 by: 2 do: [:aNumber| Transcript show: aNumber asString; cr].!
  844. !Number methodsFor: 'accessing'!
  845. identityHash
  846. ^self asString, 'n'
  847. ! !
  848. !Number methodsFor: 'arithmetic'!
  849. * aNumber
  850. "Inlined in the Compiler"
  851. <return self * aNumber>
  852. !
  853. + aNumber
  854. "Inlined in the Compiler"
  855. <return self + aNumber>
  856. !
  857. - aNumber
  858. "Inlined in the Compiler"
  859. <return self - aNumber>
  860. !
  861. / aNumber
  862. "Inlined in the Compiler"
  863. <return self / aNumber>
  864. !
  865. \\ aNumber
  866. <return self % aNumber>
  867. !
  868. abs
  869. <return Math.abs(self);>
  870. !
  871. max: aNumber
  872. <return Math.max(self, aNumber);>
  873. !
  874. min: aNumber
  875. <return Math.min(self, aNumber);>
  876. !
  877. negated
  878. ^0 - self
  879. !
  880. sqrt
  881. <return Math.sqrt(self)>
  882. !
  883. squared
  884. ^self * self
  885. ! !
  886. !Number methodsFor: 'comparing'!
  887. < aNumber
  888. "Inlined in the Compiler"
  889. <return self < aNumber>
  890. !
  891. <= aNumber
  892. "Inlined in the Compiler"
  893. <return self <= aNumber>
  894. !
  895. = aNumber
  896. <
  897. if(!! aNumber._isNumber || !! aNumber._isNumber()) {
  898. return false;
  899. }
  900. return Number(self) == aNumber
  901. >
  902. !
  903. > aNumber
  904. "Inlined in the Compiler"
  905. <return self >> aNumber>
  906. !
  907. >= aNumber
  908. "Inlined in the Compiler"
  909. <return self >>= aNumber>
  910. ! !
  911. !Number methodsFor: 'converting'!
  912. & aNumber
  913. <return self & aNumber>
  914. !
  915. @ aNumber
  916. ^Point x: self y: aNumber
  917. !
  918. asJSON
  919. ^self
  920. !
  921. asJavascript
  922. ^'(', self printString, ')'
  923. !
  924. asPoint
  925. ^Point x: self y: self
  926. !
  927. asString
  928. < return String(self) >
  929. !
  930. atRandom
  931. ^(Random new next * self) truncated + 1
  932. !
  933. rounded
  934. <return Math.round(self);>
  935. !
  936. to: aNumber
  937. | array first last count |
  938. first := self truncated.
  939. last := aNumber truncated + 1.
  940. count := 1.
  941. array := Array new.
  942. (last - first) timesRepeat: [
  943. array at: count put: first.
  944. count := count + 1.
  945. first := first + 1].
  946. ^array
  947. !
  948. to: stop by: step
  949. | array value pos |
  950. value := self.
  951. array := Array new.
  952. pos := 1.
  953. step = 0 ifTrue: [self error: 'step must be non-zero'].
  954. step < 0
  955. ifTrue: [[ value >= stop ] whileTrue: [
  956. array at: pos put: value.
  957. pos := pos + 1.
  958. value := value + step]]
  959. ifFalse: [[ value <= stop ] whileTrue: [
  960. array at: pos put: value.
  961. pos := pos + 1.
  962. value := value + step]].
  963. ^array
  964. !
  965. truncated
  966. <
  967. if(self >>= 0) {
  968. return Math.floor(self);
  969. } else {
  970. return Math.floor(self * (-1)) * (-1);
  971. };
  972. >
  973. !
  974. | aNumber
  975. <return self | aNumber>
  976. ! !
  977. !Number methodsFor: 'copying'!
  978. copy
  979. ^self
  980. !
  981. deepCopy
  982. ^self copy
  983. ! !
  984. !Number methodsFor: 'enumerating'!
  985. timesRepeat: aBlock
  986. | count |
  987. count := 1.
  988. [count > self] whileFalse: [
  989. aBlock value.
  990. count := count + 1]
  991. !
  992. to: stop by: step do: aBlock
  993. | value |
  994. value := self.
  995. step = 0 ifTrue: [self error: 'step must be non-zero'].
  996. step < 0
  997. ifTrue: [[ value >= stop ] whileTrue: [
  998. aBlock value: value.
  999. value := value + step]]
  1000. ifFalse: [[ value <= stop ] whileTrue: [
  1001. aBlock value: value.
  1002. value := value + step]]
  1003. !
  1004. to: stop do: aBlock
  1005. "Evaluate aBlock for each number from self to aNumber."
  1006. | nextValue |
  1007. nextValue := self.
  1008. [nextValue <= stop]
  1009. whileTrue:
  1010. [aBlock value: nextValue.
  1011. nextValue := nextValue + 1]
  1012. ! !
  1013. !Number methodsFor: 'printing'!
  1014. printOn: aStream
  1015. aStream nextPutAll: self asString
  1016. !
  1017. printShowingDecimalPlaces: placesDesired
  1018. <return self.toFixed(placesDesired)>
  1019. ! !
  1020. !Number methodsFor: 'testing'!
  1021. even
  1022. ^ 0 = (self \\ 2)
  1023. !
  1024. isImmutable
  1025. ^ true
  1026. !
  1027. isNumber
  1028. ^true
  1029. !
  1030. isZero
  1031. ^self = 0
  1032. !
  1033. negative
  1034. "Answer whether the receiver is mathematically negative."
  1035. ^ self < 0
  1036. !
  1037. odd
  1038. ^ self even not
  1039. !
  1040. positive
  1041. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  1042. ^ self >= 0
  1043. ! !
  1044. !Number class methodsFor: 'helios'!
  1045. heliosClass
  1046. ^ 'magnitude'
  1047. ! !
  1048. !Number class methodsFor: 'instance creation'!
  1049. pi
  1050. <return Math.PI>
  1051. ! !
  1052. Object subclass: #Organizer
  1053. instanceVariableNames: ''
  1054. package: 'Kernel-Objects'!
  1055. !Organizer commentStamp!
  1056. I represent categorization information.
  1057. ## API
  1058. Use `#addElement:` and `#removeElement:` to manipulate instances.!
  1059. !Organizer methodsFor: 'accessing'!
  1060. addElement: anObject
  1061. <self.elements.addElement(anObject)>
  1062. !
  1063. elements
  1064. ^ (self basicAt: 'elements') copy
  1065. !
  1066. removeElement: anObject
  1067. <self.elements.removeElement(anObject)>
  1068. ! !
  1069. Organizer subclass: #ClassOrganizer
  1070. instanceVariableNames: ''
  1071. package: 'Kernel-Objects'!
  1072. !ClassOrganizer commentStamp!
  1073. I am an organizer specific to classes. I hold method categorization information for classes.!
  1074. !ClassOrganizer methodsFor: 'accessing'!
  1075. addElement: aString
  1076. super addElement: aString.
  1077. SystemAnnouncer current announce: (ProtocolAdded new
  1078. protocol: aString;
  1079. theClass: self theClass;
  1080. yourself)
  1081. !
  1082. removeElement: aString
  1083. super removeElement: aString.
  1084. SystemAnnouncer current announce: (ProtocolRemoved new
  1085. protocol: aString;
  1086. theClass: self theClass;
  1087. yourself)
  1088. !
  1089. theClass
  1090. < return self.theClass >
  1091. ! !
  1092. Organizer subclass: #PackageOrganizer
  1093. instanceVariableNames: ''
  1094. package: 'Kernel-Objects'!
  1095. !PackageOrganizer commentStamp!
  1096. I am an organizer specific to packages. I hold classes categorization information.!
  1097. Object subclass: #Package
  1098. instanceVariableNames: 'extension'
  1099. package: 'Kernel-Objects'!
  1100. !Package commentStamp!
  1101. I am 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.
  1102. Each package has a name and can be queried for its classes, but it will then resort to a reverse scan of all classes to find them.
  1103. ## API
  1104. Packages are manipulated through "Smalltalk current", like for example finding one based on a name or with `Package class >> #name` directly:
  1105. Smalltalk current packageAt: 'Kernel'
  1106. Package named: 'Kernel'
  1107. 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 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.
  1108. You can fetch a package from the server:
  1109. Package load: 'Additional-Examples'!
  1110. !Package methodsFor: 'accessing'!
  1111. name
  1112. <return self.pkgName>
  1113. !
  1114. name: aString
  1115. <self.pkgName = aString>
  1116. !
  1117. organization
  1118. ^ self basicAt: 'organization'
  1119. ! !
  1120. !Package methodsFor: 'classes'!
  1121. classes
  1122. ^ self organization elements
  1123. !
  1124. setupClasses
  1125. self classes
  1126. do: [ :each | ClassBuilder new setupClass: each ];
  1127. do: [ :each | each initialize ]
  1128. !
  1129. sortedClasses
  1130. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  1131. ^self class sortedClasses: self classes
  1132. ! !
  1133. !Package methodsFor: 'printing'!
  1134. printOn: aStream
  1135. super printOn: aStream.
  1136. aStream
  1137. nextPutAll: ' (';
  1138. nextPutAll: self name;
  1139. nextPutAll: ')'
  1140. ! !
  1141. !Package methodsFor: 'testing'!
  1142. isPackage
  1143. ^ true
  1144. ! !
  1145. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  1146. !Package class methodsFor: 'accessing'!
  1147. named: aPackageName
  1148. ^Smalltalk current packageAt: aPackageName
  1149. !
  1150. named: aPackageName ifAbsent: aBlock
  1151. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  1152. ! !
  1153. !Package class methodsFor: 'loading-storing'!
  1154. load: aPackageName
  1155. self deprecatedAPI.
  1156. self load: aPackageName prefix: self defaultCommitPathJs, '/'
  1157. !
  1158. load: aPackageName prefix: aPrefix
  1159. self deprecatedAPI.
  1160. PlatformInterface ajax: #{
  1161. 'url' -> (aPrefix , aPackageName , '.js').
  1162. 'dataType' -> 'script'.
  1163. 'success' -> [
  1164. (Package named: aPackageName) setupClasses ]
  1165. }
  1166. ! !
  1167. !Package class methodsFor: 'sorting'!
  1168. sortedClasses: classes
  1169. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  1170. | children others nodes expandedClasses |
  1171. children := #().
  1172. others := #().
  1173. classes do: [:each |
  1174. (classes includes: each superclass)
  1175. ifFalse: [children add: each]
  1176. ifTrue: [others add: each]].
  1177. nodes := children collect: [:each |
  1178. ClassSorterNode on: each classes: others level: 0].
  1179. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  1180. expandedClasses := Array new.
  1181. nodes do: [:aNode |
  1182. aNode traverseClassesWith: expandedClasses].
  1183. ^expandedClasses
  1184. ! !
  1185. Object subclass: #PlatformInterface
  1186. instanceVariableNames: ''
  1187. package: 'Kernel-Objects'!
  1188. !PlatformInterface commentStamp!
  1189. I am single entry point to UI and environment interface.
  1190. My `initialize` tries several options (for now, browser environment only) to set myself up.
  1191. ## API
  1192. PlatformInterface alert: 'Hey, there is a problem'.
  1193. PlatformInterface confirm: 'Affirmative?'.
  1194. PlatformInterface prompt: 'Your name:'.
  1195. PlatformInterface ajax: #{
  1196. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  1197. }.!
  1198. PlatformInterface class instanceVariableNames: 'worker'!
  1199. !PlatformInterface class methodsFor: 'accessing'!
  1200. setWorker: anObject
  1201. worker := anObject
  1202. ! !
  1203. !PlatformInterface class methodsFor: 'actions'!
  1204. ajax: anObject
  1205. ^worker
  1206. ifNotNil: [ worker ajax: anObject ]
  1207. ifNil: [ self error: 'ajax: not available' ]
  1208. !
  1209. alert: aString
  1210. ^worker
  1211. ifNotNil: [ worker alert: aString ]
  1212. ifNil: [ self error: 'alert: not available' ]
  1213. !
  1214. confirm: aString
  1215. ^worker
  1216. ifNotNil: [ worker confirm: aString ]
  1217. ifNil: [ self error: 'confirm: not available' ]
  1218. !
  1219. existsGlobal: aString
  1220. <
  1221. var f = new Function('aString',
  1222. 'if (/^[0-9]/.test(aString) || !!/^[\\w_]+$/.test(aString))\n'+
  1223. ' return false;\n'+
  1224. 'try { eval(aString); return true; } catch (ex) {}\n'+
  1225. 'return false;');
  1226. return f(aString);
  1227. >
  1228. !
  1229. prompt: aString
  1230. ^worker
  1231. ifNotNil: [ worker prompt: aString ]
  1232. ifNil: [ self error: 'prompt: not available' ]
  1233. ! !
  1234. !PlatformInterface class methodsFor: 'initialization'!
  1235. initialize
  1236. | candidate |
  1237. super initialize.
  1238. BrowserInterface ifNotNil: [
  1239. candidate := BrowserInterface new.
  1240. candidate isAvailable ifTrue: [ self setWorker: candidate. ^self ]
  1241. ]
  1242. ! !
  1243. Object subclass: #Point
  1244. instanceVariableNames: 'x y'
  1245. package: 'Kernel-Objects'!
  1246. !Point commentStamp!
  1247. I represent an x-y pair of numbers usually designating a geometric coordinate.
  1248. ## API
  1249. Instances are traditionally created using the binary `#@` message to a number:
  1250. 100@120
  1251. Points can then be arithmetically manipulated:
  1252. 100@100 + (10@10)
  1253. ...or for example:
  1254. (100@100) * 2
  1255. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  1256. 100@ -100 "but 100@-100 would not parse"!
  1257. !Point methodsFor: 'accessing'!
  1258. x
  1259. ^x
  1260. !
  1261. x: aNumber
  1262. x := aNumber
  1263. !
  1264. y
  1265. ^y
  1266. !
  1267. y: aNumber
  1268. y := aNumber
  1269. ! !
  1270. !Point methodsFor: 'arithmetic'!
  1271. * aPoint
  1272. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  1273. !
  1274. + aPoint
  1275. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  1276. !
  1277. - aPoint
  1278. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  1279. !
  1280. / aPoint
  1281. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1282. !
  1283. = aPoint
  1284. ^aPoint class = self class and: [
  1285. (aPoint x = self x) & (aPoint y = self y)]
  1286. ! !
  1287. !Point methodsFor: 'converting'!
  1288. asPoint
  1289. ^self
  1290. ! !
  1291. !Point methodsFor: 'printing'!
  1292. printOn: aStream
  1293. "Print receiver in classic x@y notation."
  1294. x printOn: aStream.
  1295. aStream nextPutAll: '@'.
  1296. (y notNil and: [y negative]) ifTrue: [
  1297. "Avoid ambiguous @- construct"
  1298. aStream space ].
  1299. y printOn: aStream
  1300. ! !
  1301. !Point methodsFor: 'transforming'!
  1302. translateBy: delta
  1303. "Answer a Point translated by delta (an instance of Point)."
  1304. ^(delta x + x) @ (delta y + y)
  1305. ! !
  1306. !Point class methodsFor: 'helios'!
  1307. heliosClass
  1308. ^ 'magnitude'
  1309. ! !
  1310. !Point class methodsFor: 'instance creation'!
  1311. x: aNumber y: anotherNumber
  1312. ^self new
  1313. x: aNumber;
  1314. y: anotherNumber;
  1315. yourself
  1316. ! !
  1317. Object subclass: #ProgressHandler
  1318. instanceVariableNames: ''
  1319. package: 'Kernel-Objects'!
  1320. !ProgressHandler commentStamp!
  1321. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  1322. Subclasses of can register themselves as the current handler with
  1323. `ProgressHandler class >> register`.
  1324. The default behavior is to simply iterate over the collection.!
  1325. !ProgressHandler methodsFor: 'progress handling'!
  1326. do: aBlock on: aCollection displaying: aString
  1327. aCollection do: aBlock
  1328. ! !
  1329. ProgressHandler class instanceVariableNames: 'current'!
  1330. !ProgressHandler class methodsFor: 'accessing'!
  1331. current
  1332. ^current ifNil: [ current := self new ]
  1333. !
  1334. setCurrent: anHandler
  1335. current := anHandler
  1336. ! !
  1337. !ProgressHandler class methodsFor: 'initialization'!
  1338. initialize
  1339. self register
  1340. !
  1341. register
  1342. ProgressHandler setCurrent: self new
  1343. ! !
  1344. Object subclass: #Random
  1345. instanceVariableNames: ''
  1346. package: 'Kernel-Objects'!
  1347. !Random commentStamp!
  1348. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  1349. ## API
  1350. The typical use case it to use the `#next` method like the following:
  1351. Random new next
  1352. 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`
  1353. 10 atRandom
  1354. A random number in a specific interval can be obtained with the following:
  1355. (3 to: 7) atRandom
  1356. 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:
  1357. 5 atRandom + 2
  1358. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1359. #('a' 'b' 'c') atRandom
  1360. As well as letter from a `String`:
  1361. 'abc' atRandom
  1362. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1363. !Random methodsFor: 'accessing'!
  1364. next
  1365. <return Math.random()>
  1366. !
  1367. next: anInteger
  1368. ^(1 to: anInteger) collect: [:each | self next]
  1369. ! !
  1370. Object subclass: #Smalltalk
  1371. instanceVariableNames: ''
  1372. package: 'Kernel-Objects'!
  1373. !Smalltalk commentStamp!
  1374. I represent the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  1375. ## API
  1376. I have only one instance, accessed with class-side method `#current`.
  1377. The `smalltalk` object holds all class and packages defined in the system.
  1378. ## Classes
  1379. Classes can be accessed using the following methods:
  1380. - `#classes` answers the full list of Smalltalk classes in the system
  1381. - `#at:` answers a specific class or `nil`
  1382. ## Packages
  1383. Packages can be accessed using the following methods:
  1384. - `#packages` answers the full list of packages
  1385. - `#packageAt:` answers a specific package or `nil`
  1386. ## Parsing
  1387. The `#parse:` method is used to parse Amber source code.
  1388. It requires the `Compiler` package and the `js/parser.js` parser file in order to work.!
  1389. !Smalltalk methodsFor: 'accessing'!
  1390. at: aString
  1391. <return self[aString]>
  1392. !
  1393. parse: aString
  1394. | result |
  1395. self
  1396. try: [result := self basicParse: aString]
  1397. catch: [:ex | (self parseError: ex parsing: aString) signal].
  1398. ^ result
  1399. source: aString;
  1400. yourself
  1401. !
  1402. readJSObject: anObject
  1403. <return self.readJSObject(anObject)>
  1404. !
  1405. reservedWords
  1406. "JavaScript reserved words"
  1407. <return self.reservedWords>
  1408. !
  1409. version
  1410. "Answer the version string of Amber"
  1411. ^ '0.11.0'
  1412. ! !
  1413. !Smalltalk methodsFor: 'classes'!
  1414. classes
  1415. <return self.classes()>
  1416. !
  1417. deleteClass: aClass
  1418. "Deletes a class by deleting its binding only. Use #removeClass instead"
  1419. <self.removeClass(aClass)>
  1420. !
  1421. removeClass: aClass
  1422. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  1423. self deleteClass: aClass.
  1424. SystemAnnouncer current
  1425. announce: (ClassRemoved new
  1426. theClass: aClass;
  1427. yourself)
  1428. ! !
  1429. !Smalltalk methodsFor: 'error handling'!
  1430. asSmalltalkException: anObject
  1431. "A JavaScript exception may be thrown.
  1432. We then need to convert it back to a Smalltalk object"
  1433. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  1434. ifTrue: [ anObject ]
  1435. ifFalse: [ JavaScriptException on: anObject ]
  1436. !
  1437. parseError: anException parsing: aString
  1438. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  1439. ! !
  1440. !Smalltalk methodsFor: 'globals'!
  1441. addGlobalJsVariable: aString
  1442. self globalJsVariables add: aString
  1443. !
  1444. deleteGlobalJsVariable: aString
  1445. self globalJsVariables remove: aString ifAbsent:[]
  1446. !
  1447. globalJsVariables
  1448. "Array of global JavaScript variables"
  1449. <return self.globalJsVariables>
  1450. ! !
  1451. !Smalltalk methodsFor: 'packages'!
  1452. createPackage: packageName
  1453. "Create and bind a new package with given name and return it."
  1454. <return smalltalk.addPackage(packageName)>
  1455. !
  1456. deletePackage: packageName
  1457. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  1458. To remove a package, use #removePackage instead."
  1459. <delete smalltalk.packages[packageName]>
  1460. !
  1461. packageAt: packageName
  1462. <return self.packages[packageName]>
  1463. !
  1464. packageAt: packageName ifAbsent: aBlock
  1465. ^(self packageAt: packageName) ifNil: aBlock
  1466. !
  1467. packages
  1468. "Return all Package instances in the system."
  1469. <return self.packages.all()>
  1470. !
  1471. pseudoVariableNames
  1472. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  1473. !
  1474. removePackage: packageName
  1475. "Removes a package and all its classes."
  1476. | pkg |
  1477. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1478. pkg classes do: [:each |
  1479. self removeClass: each].
  1480. self deletePackage: packageName
  1481. !
  1482. renamePackage: packageName to: newName
  1483. "Rename a package."
  1484. | pkg |
  1485. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1486. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  1487. (self basicAt: 'packages') at: newName put: pkg.
  1488. pkg name: newName.
  1489. self deletePackage: packageName.
  1490. ! !
  1491. !Smalltalk methodsFor: 'private'!
  1492. basicParse: aString
  1493. <return smalltalk.parser.parse(aString)>
  1494. !
  1495. createPackage: packageName properties: aDict
  1496. "Needed to import .st files: they begin with this call."
  1497. self deprecatedAPI.
  1498. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  1499. ^ self createPackage: packageName
  1500. ! !
  1501. !Smalltalk methodsFor: 'testing'!
  1502. isSmalltalkObject: anObject
  1503. "Consider anObject a Smalltalk object if it has a 'klass' property.
  1504. Note that this may be unaccurate"
  1505. <return typeof anObject.klass !!== 'undefined'>
  1506. ! !
  1507. Smalltalk class instanceVariableNames: 'current'!
  1508. !Smalltalk class methodsFor: 'accessing'!
  1509. current
  1510. <return smalltalk>
  1511. ! !
  1512. Object subclass: #Timeout
  1513. instanceVariableNames: 'rawTimeout'
  1514. package: 'Kernel-Objects'!
  1515. !Timeout commentStamp!
  1516. I am wrapping the returns from `set{Timeout,Interval}`.
  1517. ## Motivation
  1518. Number suffices in browsers, but node.js returns an object.!
  1519. !Timeout methodsFor: 'accessing'!
  1520. rawTimeout: anObject
  1521. rawTimeout := anObject
  1522. ! !
  1523. !Timeout methodsFor: 'timeout/interval'!
  1524. clearInterval
  1525. <
  1526. var interval = self["@rawTimeout"];
  1527. clearInterval(interval);
  1528. >
  1529. !
  1530. clearTimeout
  1531. <
  1532. var timeout = self["@rawTimeout"];
  1533. clearTimeout(timeout);
  1534. >
  1535. ! !
  1536. !Timeout class methodsFor: 'instance creation'!
  1537. on: anObject
  1538. ^self new rawTimeout: anObject; yourself
  1539. ! !
  1540. Object subclass: #UndefinedObject
  1541. instanceVariableNames: ''
  1542. package: 'Kernel-Objects'!
  1543. !UndefinedObject commentStamp!
  1544. I describe the behavior of my sole instance, `nil`. `nil` represents a prior value for variables that have not been initialized, or for results which are meaningless.
  1545. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  1546. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  1547. !UndefinedObject methodsFor: 'class creation'!
  1548. subclass: aString instanceVariableNames: anotherString
  1549. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1550. !
  1551. subclass: aString instanceVariableNames: aString2 category: aString3
  1552. "Kept for compatibility."
  1553. self deprecatedAPI.
  1554. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1555. !
  1556. subclass: aString instanceVariableNames: aString2 package: aString3
  1557. ^ClassBuilder new
  1558. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  1559. ! !
  1560. !UndefinedObject methodsFor: 'converting'!
  1561. asJSON
  1562. ^null
  1563. ! !
  1564. !UndefinedObject methodsFor: 'copying'!
  1565. deepCopy
  1566. ^self
  1567. !
  1568. shallowCopy
  1569. ^self
  1570. ! !
  1571. !UndefinedObject methodsFor: 'printing'!
  1572. printOn: aStream
  1573. aStream nextPutAll: 'nil'
  1574. ! !
  1575. !UndefinedObject methodsFor: 'testing'!
  1576. ifNil: aBlock
  1577. "inlined in the Compiler"
  1578. ^self ifNil: aBlock ifNotNil: []
  1579. !
  1580. ifNil: aBlock ifNotNil: anotherBlock
  1581. "inlined in the Compiler"
  1582. ^aBlock value
  1583. !
  1584. ifNotNil: aBlock
  1585. "inlined in the Compiler"
  1586. ^self
  1587. !
  1588. ifNotNil: aBlock ifNil: anotherBlock
  1589. "inlined in the Compiler"
  1590. ^anotherBlock value
  1591. !
  1592. isImmutable
  1593. ^ true
  1594. !
  1595. isNil
  1596. ^true
  1597. !
  1598. notNil
  1599. ^false
  1600. ! !
  1601. !UndefinedObject class methodsFor: 'instance creation'!
  1602. new
  1603. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1604. ! !
  1605. !String methodsFor: '*Kernel-Objects'!
  1606. asJavaScriptSelector
  1607. "Return first keyword of the selector, without trailing colon."
  1608. ^self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  1609. ! !