Kernel-Objects.st 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  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()} catch(e) {return anotherBlock(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: 'initialization'!
  277. initialize
  278. "no op"
  279. ! !
  280. Object subclass: #Boolean
  281. instanceVariableNames: ''
  282. package: 'Kernel-Objects'!
  283. !Boolean commentStamp!
  284. I define the protocol for logic testing operations and conditional control structures for the logical values (see the `controlling` protocol).
  285. I have two instances, `true` and `false`.
  286. I am directly mapped to JavaScript Boolean. The `true` and `false` objects are the JavaScript boolean objects.
  287. ## Usage Example:
  288. aBoolean not ifTrue: [ ... ] ifFalse: [ ... ]!
  289. !Boolean methodsFor: 'comparing'!
  290. = aBoolean
  291. <
  292. if(!! aBoolean._isBoolean || !! aBoolean._isBoolean()) {
  293. return false;
  294. }
  295. return Boolean(self == true) == aBoolean
  296. >
  297. !
  298. == aBoolean
  299. ^self = aBoolean
  300. ! !
  301. !Boolean methodsFor: 'controlling'!
  302. & aBoolean
  303. <
  304. if(self == true) {
  305. return aBoolean;
  306. } else {
  307. return false;
  308. }
  309. >
  310. !
  311. and: aBlock
  312. ^self = true
  313. ifTrue: aBlock
  314. ifFalse: [false]
  315. !
  316. ifFalse: aBlock
  317. "inlined in the Compiler"
  318. ^self ifTrue: [] ifFalse: aBlock
  319. !
  320. ifFalse: aBlock ifTrue: anotherBlock
  321. "inlined in the Compiler"
  322. ^self ifTrue: anotherBlock ifFalse: aBlock
  323. !
  324. ifTrue: aBlock
  325. "inlined in the Compiler"
  326. ^self ifTrue: aBlock ifFalse: []
  327. !
  328. ifTrue: aBlock ifFalse: anotherBlock
  329. "inlined in the Compiler"
  330. <
  331. if(self == true) {
  332. return aBlock();
  333. } else {
  334. return anotherBlock();
  335. }
  336. >
  337. !
  338. not
  339. ^self = false
  340. !
  341. or: aBlock
  342. ^self = true
  343. ifTrue: [true]
  344. ifFalse: aBlock
  345. !
  346. | aBoolean
  347. <
  348. if(self == true) {
  349. return true;
  350. } else {
  351. return aBoolean;
  352. }
  353. >
  354. ! !
  355. !Boolean methodsFor: 'converting'!
  356. asJSON
  357. ^self
  358. !
  359. asString
  360. < return self.toString() >
  361. ! !
  362. !Boolean methodsFor: 'copying'!
  363. deepCopy
  364. ^self
  365. !
  366. shallowCopy
  367. ^self
  368. ! !
  369. !Boolean methodsFor: 'printing'!
  370. printOn: aStream
  371. aStream nextPutAll: self asString
  372. ! !
  373. !Boolean methodsFor: 'testing'!
  374. isBoolean
  375. ^ true
  376. !
  377. isImmutable
  378. ^ true
  379. ! !
  380. Object subclass: #Date
  381. instanceVariableNames: ''
  382. package: 'Kernel-Objects'!
  383. !Date commentStamp!
  384. I am used to work with both dates and times. Therefore `Date today` and `Date now` are both valid in
  385. Amber and answer the same date object.
  386. Date directly maps to the `Date()` JavaScript constructor, and Amber date objects are JavaScript date objects.
  387. ## API
  388. The class-side `instance creation` protocol contains some convenience methods for creating date/time objects such as `#fromSeconds:`.
  389. Arithmetic and comparison is supported (see the `comparing` and `arithmetic` protocols).
  390. The `converting` protocol provides convenience methods for various convertions (to numbers, strings, etc.).!
  391. !Date methodsFor: 'accessing'!
  392. day
  393. ^self dayOfWeek
  394. !
  395. day: aNumber
  396. self dayOfWeek: aNumber
  397. !
  398. dayOfMonth
  399. <return self.getDate()>
  400. !
  401. dayOfMonth: aNumber
  402. <self.setDate(aNumber)>
  403. !
  404. dayOfWeek
  405. <return self.getDay() + 1>
  406. !
  407. dayOfWeek: aNumber
  408. <return self.setDay(aNumber - 1)>
  409. !
  410. hours
  411. <return self.getHours()>
  412. !
  413. hours: aNumber
  414. <self.setHours(aNumber)>
  415. !
  416. milliseconds
  417. <return self.getMilliseconds()>
  418. !
  419. milliseconds: aNumber
  420. <self.setMilliseconds(aNumber)>
  421. !
  422. minutes
  423. <return self.getMinutes()>
  424. !
  425. minutes: aNumber
  426. <self.setMinutes(aNumber)>
  427. !
  428. month
  429. <return self.getMonth() + 1>
  430. !
  431. month: aNumber
  432. <self.setMonth(aNumber - 1)>
  433. !
  434. seconds
  435. <return self.getSeconds()>
  436. !
  437. seconds: aNumber
  438. <self.setSeconds(aNumber)>
  439. !
  440. time
  441. <return self.getTime()>
  442. !
  443. time: aNumber
  444. <self.setTime(aNumber)>
  445. !
  446. year
  447. <return self.getFullYear()>
  448. !
  449. year: aNumber
  450. <self.setFullYear(aNumber)>
  451. ! !
  452. !Date methodsFor: 'arithmetic'!
  453. + aDate
  454. <return self + aDate>
  455. !
  456. - aDate
  457. <return self - aDate>
  458. ! !
  459. !Date methodsFor: 'comparing'!
  460. < aDate
  461. <return self < aDate>
  462. !
  463. <= aDate
  464. <return self <= aDate>
  465. !
  466. > aDate
  467. <return self >> aDate>
  468. !
  469. >= aDate
  470. <return self >>= aDate>
  471. ! !
  472. !Date methodsFor: 'converting'!
  473. asDateString
  474. <return self.toDateString()>
  475. !
  476. asLocaleString
  477. <return self.toLocaleString()>
  478. !
  479. asMilliseconds
  480. ^self time
  481. !
  482. asNumber
  483. ^self asMilliseconds
  484. !
  485. asString
  486. <return self.toString()>
  487. !
  488. asTimeString
  489. <return self.toTimeString()>
  490. ! !
  491. !Date methodsFor: 'printing'!
  492. printOn: aStream
  493. aStream nextPutAll: self asString
  494. ! !
  495. !Date class methodsFor: 'instance creation'!
  496. fromMilliseconds: aNumber
  497. ^self new: aNumber
  498. !
  499. fromSeconds: aNumber
  500. ^self fromMilliseconds: aNumber * 1000
  501. !
  502. fromString: aString
  503. "Example: Date fromString('2011/04/15 00:00:00')"
  504. ^self new: aString
  505. !
  506. millisecondsToRun: aBlock
  507. | t |
  508. t := Date now.
  509. aBlock value.
  510. ^Date now - t
  511. !
  512. new: anObject
  513. <return new Date(anObject)>
  514. !
  515. now
  516. ^self today
  517. !
  518. today
  519. ^self new
  520. ! !
  521. Object subclass: #Environment
  522. instanceVariableNames: ''
  523. package: 'Kernel-Objects'!
  524. !Environment commentStamp!
  525. I provide an unified entry point to manipulate Amber packages, classes and methods.
  526. Typical use cases include IDEs, remote access and restricting browsing.!
  527. !Environment methodsFor: 'accessing'!
  528. allSelectors
  529. ^ (Smalltalk current at: 'allSelectors') value
  530. !
  531. availableClassNames
  532. ^ Smalltalk current classes
  533. collect: [ :each | each name ]
  534. !
  535. availablePackageNames
  536. ^ Smalltalk current packages
  537. collect: [ :each | each name ]
  538. !
  539. availableProtocolsFor: aClass
  540. | protocols |
  541. protocols := aClass protocols.
  542. aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
  543. ^ protocols asSet asArray
  544. !
  545. classBuilder
  546. ^ ClassBuilder new
  547. !
  548. classNamed: aString
  549. ^ (Smalltalk current at: aString asSymbol)
  550. ifNil: [ self error: 'Invalid class name' ]
  551. !
  552. classes
  553. ^ Smalltalk current classes
  554. !
  555. packages
  556. ^ Smalltalk current packages
  557. !
  558. systemAnnouncer
  559. ^ (Smalltalk current at: #SystemAnnouncer) current
  560. ! !
  561. !Environment methodsFor: 'actions'!
  562. commitPackage: aPackage
  563. aPackage commit
  564. !
  565. copyClass: aClass to: aClassName
  566. (Smalltalk current at: aClassName)
  567. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  568. ClassBuilder new copyClass: aClass named: aClassName
  569. !
  570. eval: aString on: aReceiver
  571. | compiler |
  572. compiler := Compiler new.
  573. [ compiler parseExpression: aString ] on: Error do: [ :ex |
  574. ^ window alert: ex messageText ].
  575. ^ compiler evaluateExpression: aString on: aReceiver
  576. !
  577. moveClass: aClass toPackage: aPackageName
  578. | package |
  579. package := Package named: aPackageName.
  580. package ifNil: [ self error: 'Invalid package name' ].
  581. package == aClass package ifTrue: [ ^ self ].
  582. aClass package: package
  583. !
  584. moveMethod: aMethod toClass: aClassName
  585. | destinationClass |
  586. destinationClass := Smalltalk current at: aClassName asSymbol.
  587. destinationClass ifNil: [ self error: 'Invalid class name' ].
  588. destinationClass == aMethod methodClass ifTrue: [ ^ self ].
  589. destinationClass
  590. compile: aMethod source
  591. category: aMethod protocol.
  592. aMethod methodClass
  593. removeCompiledMethod: aMethod
  594. !
  595. moveMethod: aMethod toProtocol: aProtocol
  596. aMethod category: aProtocol
  597. !
  598. removeClass: aClass
  599. Smalltalk current removeClass: aClass
  600. !
  601. removeMethod: aMethod
  602. aMethod methodClass removeCompiledMethod: aMethod
  603. !
  604. removeProtocol: aString from: aClass
  605. (aClass methods
  606. select: [ :each | each protocol = aString ])
  607. do: [ :each | aClass removeCompiledMethod: each ]
  608. !
  609. renameClass: aClass to: aClassName
  610. (Smalltalk current at: aClassName)
  611. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  612. ClassBuilder new renameClass: aClass to: aClassName
  613. !
  614. renameProtocol: aString to: anotherString in: aClass
  615. (aClass methods
  616. select: [ :each | each protocol = aString ])
  617. do: [ :each | each protocol: anotherString ]
  618. !
  619. setClassCommentOf: aClass to: aString
  620. aClass comment: aString
  621. ! !
  622. !Environment methodsFor: 'compiling'!
  623. addInstVarNamed: aString to: aClass
  624. self classBuilder
  625. addSubclassOf: aClass superclass
  626. named: aClass name
  627. instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
  628. package: aClass package name
  629. !
  630. compileClassComment: aString for: aClass
  631. aClass comment: aString
  632. !
  633. compileClassDefinition: aString
  634. self eval: aString on: DoIt new
  635. !
  636. compileMethod: sourceCode for: class protocol: protocol
  637. ^ class
  638. compile: sourceCode
  639. category: protocol
  640. ! !
  641. Object subclass: #InspectorHandler
  642. instanceVariableNames: ''
  643. package: 'Kernel-Objects'!
  644. !InspectorHandler commentStamp!
  645. I am responsible for inspecting object.
  646. My class-side `inspector` inst var holds the current inspector I'm delegating object inspection to.
  647. The default inspector object is the transcript.!
  648. InspectorHandler class instanceVariableNames: 'inspector'!
  649. !InspectorHandler class methodsFor: 'accessing'!
  650. inspector
  651. ^ inspector ifNil: [ inspector := Transcript ]
  652. ! !
  653. !InspectorHandler class methodsFor: 'registration'!
  654. inspect: anObject
  655. ^ self inspector inspect: anObject
  656. !
  657. register: anInspector
  658. ^ inspector := anInspector
  659. ! !
  660. Object subclass: #JSObjectProxy
  661. instanceVariableNames: 'jsObject'
  662. package: 'Kernel-Objects'!
  663. !JSObjectProxy commentStamp!
  664. I handle sending messages to JavaScript objects, making JavaScript object accessing from Amber fully transparent.
  665. My instances make intensive use of `#doesNotUnderstand:`.
  666. My instances are automatically created by Amber whenever a message is sent to a JavaScript object.
  667. ## Usage examples
  668. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  669. window alert: 'hello world'.
  670. window inspect.
  671. (window jQuery: 'body') append: 'hello world'
  672. 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.
  673. ## Message conversion rules
  674. - `someUser name` becomes `someUser.name`
  675. - `someUser name: 'John'` becomes `someUser name = "John"`
  676. - `console log: 'hello world'` becomes `console.log('hello world')`
  677. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  678. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  679. !JSObjectProxy methodsFor: 'accessing'!
  680. at: aString
  681. <return self['@jsObject'][aString]>
  682. !
  683. at: aString ifAbsent: aBlock
  684. "return the aString property or evaluate aBlock if the property is not defined on the object"
  685. <
  686. var obj = self['@jsObject'];
  687. return aString in obj ? obj[aString] : aBlock();
  688. >
  689. !
  690. at: aString ifPresent: aBlock
  691. "return the evaluation of aBlock with the value if the property is defined or return nil"
  692. <
  693. var obj = self['@jsObject'];
  694. return aString in obj ? aBlock(obj[aString]) : nil;
  695. >
  696. !
  697. at: aString ifPresent: aBlock ifAbsent: anotherBlock
  698. "return the evaluation of aBlock with the value if the property is defined
  699. or return value of anotherBlock"
  700. <
  701. var obj = self['@jsObject'];
  702. return aString in obj ? aBlock(obj[aString]) : anotherBlock();
  703. >
  704. !
  705. at: aString put: anObject
  706. <self['@jsObject'][aString] = anObject>
  707. !
  708. jsObject
  709. ^jsObject
  710. !
  711. jsObject: aJSObject
  712. jsObject := aJSObject
  713. !
  714. lookupProperty: aString
  715. "Looks up a property in JS object.
  716. Answer the property if it is present, or nil if it is not present."
  717. <return aString in self._jsObject() ? aString : nil>
  718. !
  719. value
  720. "if attribute 'value' exists on the JS object return it,
  721. otherwise return the result of Object>>value."
  722. ^ self
  723. at: 'value'
  724. ifAbsent: [ super value ]
  725. ! !
  726. !JSObjectProxy methodsFor: 'enumerating'!
  727. keysAndValuesDo: aBlock
  728. <
  729. var o = self['@jsObject'];
  730. for(var i in o) {
  731. aBlock(i, o[i]);
  732. }
  733. >
  734. ! !
  735. !JSObjectProxy methodsFor: 'printing'!
  736. printOn: aStream
  737. aStream nextPutAll: self jsObject toString
  738. ! !
  739. !JSObjectProxy methodsFor: 'proxy'!
  740. addObjectVariablesTo: aDictionary
  741. <
  742. for(var i in self['@jsObject']) {
  743. aDictionary._at_put_(i, self['@jsObject'][i]);
  744. }
  745. >
  746. !
  747. doesNotUnderstand: aMessage
  748. ^ (self lookupProperty: aMessage selector asJavaScriptSelector)
  749. ifNil: [ super doesNotUnderstand: aMessage ]
  750. ifNotNil: [ :jsSelector |
  751. self
  752. forwardMessage: jsSelector
  753. withArguments: aMessage arguments ]
  754. !
  755. forwardMessage: aString withArguments: anArray
  756. <
  757. return smalltalk.send(self._jsObject(), aString, anArray);
  758. >
  759. !
  760. inspectOn: anInspector
  761. | variables |
  762. variables := Dictionary new.
  763. variables at: '#self' put: self jsObject.
  764. anInspector setLabel: self printString.
  765. self addObjectVariablesTo: variables.
  766. anInspector setVariables: variables
  767. ! !
  768. !JSObjectProxy class methodsFor: 'instance creation'!
  769. on: aJSObject
  770. ^self new
  771. jsObject: aJSObject;
  772. yourself
  773. ! !
  774. Object subclass: #Number
  775. instanceVariableNames: ''
  776. package: 'Kernel-Objects'!
  777. !Number commentStamp!
  778. I am the Amber representation for all numbers.
  779. I am directly mapped to JavaScript Number.
  780. ## API
  781. I provide all necessary methods for arithmetic operations, comparison, conversion and so on with numbers.
  782. My instances can also be used to evaluate a block a fixed number of times:
  783. 5 timesRepeat: [Transcript show: 'This will be printed 5 times'; cr].
  784. 1 to: 5 do: [:aNumber| Transcript show: aNumber asString; cr].
  785. 1 to: 10 by: 2 do: [:aNumber| Transcript show: aNumber asString; cr].!
  786. !Number methodsFor: 'accessing'!
  787. identityHash
  788. ^self asString, 'n'
  789. ! !
  790. !Number methodsFor: 'arithmetic'!
  791. * aNumber
  792. "Inlined in the Compiler"
  793. <return self * aNumber>
  794. !
  795. + aNumber
  796. "Inlined in the Compiler"
  797. <return self + aNumber>
  798. !
  799. - aNumber
  800. "Inlined in the Compiler"
  801. <return self - aNumber>
  802. !
  803. / aNumber
  804. "Inlined in the Compiler"
  805. <return self / aNumber>
  806. !
  807. \\ aNumber
  808. <return self % aNumber>
  809. !
  810. abs
  811. <return Math.abs(self);>
  812. !
  813. max: aNumber
  814. <return Math.max(self, aNumber);>
  815. !
  816. min: aNumber
  817. <return Math.min(self, aNumber);>
  818. !
  819. negated
  820. ^0 - self
  821. !
  822. sqrt
  823. <return Math.sqrt(self)>
  824. !
  825. squared
  826. ^self * self
  827. ! !
  828. !Number methodsFor: 'comparing'!
  829. < aNumber
  830. "Inlined in the Compiler"
  831. <return self < aNumber>
  832. !
  833. <= aNumber
  834. "Inlined in the Compiler"
  835. <return self <= aNumber>
  836. !
  837. = aNumber
  838. <
  839. if(!! aNumber._isNumber || !! aNumber._isNumber()) {
  840. return false;
  841. }
  842. return Number(self) == aNumber
  843. >
  844. !
  845. > aNumber
  846. "Inlined in the Compiler"
  847. <return self >> aNumber>
  848. !
  849. >= aNumber
  850. "Inlined in the Compiler"
  851. <return self >>= aNumber>
  852. ! !
  853. !Number methodsFor: 'converting'!
  854. & aNumber
  855. <return self & aNumber>
  856. !
  857. @ aNumber
  858. ^Point x: self y: aNumber
  859. !
  860. asJSON
  861. ^self
  862. !
  863. asJavascript
  864. ^'(', self printString, ')'
  865. !
  866. asPoint
  867. ^Point x: self y: self
  868. !
  869. asString
  870. < return String(self) >
  871. !
  872. atRandom
  873. ^(Random new next * self) truncated + 1
  874. !
  875. rounded
  876. <return Math.round(self);>
  877. !
  878. to: aNumber
  879. | array first last count |
  880. first := self truncated.
  881. last := aNumber truncated + 1.
  882. count := 1.
  883. array := Array new.
  884. (last - first) timesRepeat: [
  885. array at: count put: first.
  886. count := count + 1.
  887. first := first + 1].
  888. ^array
  889. !
  890. to: stop by: step
  891. | array value pos |
  892. value := self.
  893. array := Array new.
  894. pos := 1.
  895. step = 0 ifTrue: [self error: 'step must be non-zero'].
  896. step < 0
  897. ifTrue: [[ value >= stop ] whileTrue: [
  898. array at: pos put: value.
  899. pos := pos + 1.
  900. value := value + step]]
  901. ifFalse: [[ value <= stop ] whileTrue: [
  902. array at: pos put: value.
  903. pos := pos + 1.
  904. value := value + step]].
  905. ^array
  906. !
  907. truncated
  908. <
  909. if(self >>= 0) {
  910. return Math.floor(self);
  911. } else {
  912. return Math.floor(self * (-1)) * (-1);
  913. };
  914. >
  915. !
  916. | aNumber
  917. <return self | aNumber>
  918. ! !
  919. !Number methodsFor: 'copying'!
  920. copy
  921. ^self
  922. !
  923. deepCopy
  924. ^self copy
  925. ! !
  926. !Number methodsFor: 'enumerating'!
  927. timesRepeat: aBlock
  928. | count |
  929. count := 1.
  930. [count > self] whileFalse: [
  931. aBlock value.
  932. count := count + 1]
  933. !
  934. to: stop by: step do: aBlock
  935. | value |
  936. value := self.
  937. step = 0 ifTrue: [self error: 'step must be non-zero'].
  938. step < 0
  939. ifTrue: [[ value >= stop ] whileTrue: [
  940. aBlock value: value.
  941. value := value + step]]
  942. ifFalse: [[ value <= stop ] whileTrue: [
  943. aBlock value: value.
  944. value := value + step]]
  945. !
  946. to: stop do: aBlock
  947. "Evaluate aBlock for each number from self to aNumber."
  948. | nextValue |
  949. nextValue := self.
  950. [nextValue <= stop]
  951. whileTrue:
  952. [aBlock value: nextValue.
  953. nextValue := nextValue + 1]
  954. ! !
  955. !Number methodsFor: 'printing'!
  956. printOn: aStream
  957. aStream nextPutAll: self asString
  958. !
  959. printShowingDecimalPlaces: placesDesired
  960. <return self.toFixed(placesDesired)>
  961. ! !
  962. !Number methodsFor: 'testing'!
  963. even
  964. ^ 0 = (self \\ 2)
  965. !
  966. isImmutable
  967. ^ true
  968. !
  969. isNumber
  970. ^true
  971. !
  972. isZero
  973. ^self = 0
  974. !
  975. negative
  976. "Answer whether the receiver is mathematically negative."
  977. ^ self < 0
  978. !
  979. odd
  980. ^ self even not
  981. !
  982. positive
  983. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  984. ^ self >= 0
  985. ! !
  986. !Number class methodsFor: 'instance creation'!
  987. pi
  988. <return Math.PI>
  989. ! !
  990. Object subclass: #Organizer
  991. instanceVariableNames: ''
  992. package: 'Kernel-Objects'!
  993. !Organizer commentStamp!
  994. I represent categorization information.
  995. ## API
  996. Use `#addElement:` and `#removeElement:` to manipulate instances.!
  997. !Organizer methodsFor: 'accessing'!
  998. addElement: anObject
  999. <self.elements.addElement(anObject)>
  1000. !
  1001. elements
  1002. ^ (self basicAt: 'elements') copy
  1003. !
  1004. removeElement: anObject
  1005. <self.elements.removeElement(anObject)>
  1006. ! !
  1007. Organizer subclass: #ClassOrganizer
  1008. instanceVariableNames: ''
  1009. package: 'Kernel-Objects'!
  1010. !ClassOrganizer commentStamp!
  1011. I am an organizer specific to classes. I hold method categorization information for classes.!
  1012. !ClassOrganizer methodsFor: 'accessing'!
  1013. addElement: aString
  1014. super addElement: aString.
  1015. SystemAnnouncer current announce: (ProtocolAdded new
  1016. protocol: aString;
  1017. theClass: self theClass;
  1018. yourself)
  1019. !
  1020. removeElement: aString
  1021. super removeElement: aString.
  1022. SystemAnnouncer current announce: (ProtocolRemoved new
  1023. protocol: aString;
  1024. theClass: self theClass;
  1025. yourself)
  1026. !
  1027. theClass
  1028. < return self.theClass >
  1029. ! !
  1030. Organizer subclass: #PackageOrganizer
  1031. instanceVariableNames: ''
  1032. package: 'Kernel-Objects'!
  1033. !PackageOrganizer commentStamp!
  1034. I am an organizer specific to packages. I hold classes categorization information.!
  1035. Object subclass: #Package
  1036. instanceVariableNames: 'commitPathJs commitPathSt'
  1037. package: 'Kernel-Objects'!
  1038. !Package commentStamp!
  1039. 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.
  1040. 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.
  1041. ## API
  1042. Packages are manipulated through "Smalltalk current", like for example finding one based on a name or with `Package class >> #name` directly:
  1043. Smalltalk current packageAt: 'Kernel'
  1044. Package named: 'Kernel'
  1045. 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.
  1046. You can fetch a package from the server:
  1047. Package load: 'Additional-Examples'!
  1048. !Package methodsFor: 'accessing'!
  1049. commitPathJs
  1050. ^ commitPathJs ifNil: [self class defaultCommitPathJs]
  1051. !
  1052. commitPathJs: aString
  1053. commitPathJs := aString
  1054. !
  1055. commitPathSt
  1056. ^ commitPathSt ifNil: [self class defaultCommitPathSt]
  1057. !
  1058. commitPathSt: aString
  1059. commitPathSt := aString
  1060. !
  1061. name
  1062. <return self.pkgName>
  1063. !
  1064. name: aString
  1065. <self.pkgName = aString>
  1066. !
  1067. organization
  1068. ^ self basicAt: 'organization'
  1069. ! !
  1070. !Package methodsFor: 'classes'!
  1071. classes
  1072. ^ self organization elements
  1073. !
  1074. setupClasses
  1075. self classes
  1076. do: [ :each | ClassBuilder new setupClass: each ];
  1077. do: [ :each | each initialize ]
  1078. !
  1079. sortedClasses
  1080. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  1081. ^self class sortedClasses: self classes
  1082. ! !
  1083. !Package methodsFor: 'printing'!
  1084. printOn: aStream
  1085. super printOn: aStream.
  1086. aStream
  1087. nextPutAll: ' (';
  1088. nextPutAll: self name;
  1089. nextPutAll: ')'
  1090. ! !
  1091. !Package methodsFor: 'testing'!
  1092. isPackage
  1093. ^ true
  1094. ! !
  1095. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  1096. !Package class methodsFor: 'accessing'!
  1097. named: aPackageName
  1098. ^Smalltalk current packageAt: aPackageName
  1099. !
  1100. named: aPackageName ifAbsent: aBlock
  1101. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  1102. ! !
  1103. !Package class methodsFor: 'commit paths'!
  1104. commitPathsFromLoader
  1105. <
  1106. var commitPath = typeof amber !!== 'undefined' && amber.commitPath;
  1107. if (!!commitPath) return;
  1108. if (commitPath.js) self._defaultCommitPathJs_(commitPath.js);
  1109. if (commitPath.st) self._defaultCommitPathSt_(commitPath.st);
  1110. >
  1111. !
  1112. defaultCommitPathJs
  1113. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  1114. !
  1115. defaultCommitPathJs: aString
  1116. defaultCommitPathJs := aString
  1117. !
  1118. defaultCommitPathSt
  1119. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  1120. !
  1121. defaultCommitPathSt: aString
  1122. defaultCommitPathSt := aString
  1123. !
  1124. resetCommitPaths
  1125. defaultCommitPathJs := nil.
  1126. defaultCommitPathSt := nil.
  1127. ! !
  1128. !Package class methodsFor: 'initialization'!
  1129. initialize
  1130. super initialize.
  1131. self commitPathsFromLoader
  1132. ! !
  1133. !Package class methodsFor: 'loading-storing'!
  1134. load: aPackageName
  1135. self load: aPackageName prefix: self defaultCommitPathJs, '/'
  1136. !
  1137. load: aPackageName prefix: aPrefix
  1138. jQuery
  1139. getScript: (aPrefix , aPackageName , '.js')
  1140. onSuccess: [
  1141. (Package named: aPackageName) setupClasses ]
  1142. ! !
  1143. !Package class methodsFor: 'sorting'!
  1144. sortedClasses: classes
  1145. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  1146. | children others nodes expandedClasses |
  1147. children := #().
  1148. others := #().
  1149. classes do: [:each |
  1150. (classes includes: each superclass)
  1151. ifFalse: [children add: each]
  1152. ifTrue: [others add: each]].
  1153. nodes := children collect: [:each |
  1154. ClassSorterNode on: each classes: others level: 0].
  1155. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  1156. expandedClasses := Array new.
  1157. nodes do: [:aNode |
  1158. aNode traverseClassesWith: expandedClasses].
  1159. ^expandedClasses
  1160. ! !
  1161. Object subclass: #Point
  1162. instanceVariableNames: 'x y'
  1163. package: 'Kernel-Objects'!
  1164. !Point commentStamp!
  1165. I represent an x-y pair of numbers usually designating a geometric coordinate.
  1166. ## API
  1167. Instances are traditionally created using the binary `#@` message to a number:
  1168. 100@120
  1169. Points can then be arithmetically manipulated:
  1170. 100@100 + (10@10)
  1171. ...or for example:
  1172. (100@100) * 2
  1173. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  1174. 100@ -100 "but 100@-100 would not parse"!
  1175. !Point methodsFor: 'accessing'!
  1176. x
  1177. ^x
  1178. !
  1179. x: aNumber
  1180. x := aNumber
  1181. !
  1182. y
  1183. ^y
  1184. !
  1185. y: aNumber
  1186. y := aNumber
  1187. ! !
  1188. !Point methodsFor: 'arithmetic'!
  1189. * aPoint
  1190. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  1191. !
  1192. + aPoint
  1193. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  1194. !
  1195. - aPoint
  1196. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  1197. !
  1198. / aPoint
  1199. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1200. !
  1201. = aPoint
  1202. ^aPoint class = self class and: [
  1203. (aPoint x = self x) & (aPoint y = self y)]
  1204. ! !
  1205. !Point methodsFor: 'converting'!
  1206. asPoint
  1207. ^self
  1208. ! !
  1209. !Point methodsFor: 'printing'!
  1210. printOn: aStream
  1211. "Print receiver in classic x@y notation."
  1212. x printOn: aStream.
  1213. aStream nextPutAll: '@'.
  1214. (y notNil and: [y negative]) ifTrue: [
  1215. "Avoid ambiguous @- construct"
  1216. aStream space ].
  1217. y printOn: aStream
  1218. ! !
  1219. !Point methodsFor: 'transforming'!
  1220. translateBy: delta
  1221. "Answer a Point translated by delta (an instance of Point)."
  1222. ^(delta x + x) @ (delta y + y)
  1223. ! !
  1224. !Point class methodsFor: 'instance creation'!
  1225. x: aNumber y: anotherNumber
  1226. ^self new
  1227. x: aNumber;
  1228. y: anotherNumber;
  1229. yourself
  1230. ! !
  1231. Object subclass: #Random
  1232. instanceVariableNames: ''
  1233. package: 'Kernel-Objects'!
  1234. !Random commentStamp!
  1235. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  1236. ## API
  1237. The typical use case it to use the `#next` method like the following:
  1238. Random new next
  1239. 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`
  1240. 10 atRandom
  1241. A random number in a specific interval can be obtained with the following:
  1242. (3 to: 7) atRandom
  1243. 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:
  1244. 5 atRandom + 2
  1245. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1246. #('a' 'b' 'c') atRandom
  1247. As well as letter from a `String`:
  1248. 'abc' atRandom
  1249. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1250. !Random methodsFor: 'accessing'!
  1251. next
  1252. <return Math.random()>
  1253. !
  1254. next: anInteger
  1255. ^(1 to: anInteger) collect: [:each | self next]
  1256. ! !
  1257. Object subclass: #Smalltalk
  1258. instanceVariableNames: ''
  1259. package: 'Kernel-Objects'!
  1260. !Smalltalk commentStamp!
  1261. I represent the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  1262. ## API
  1263. I have only one instance, accessed with class-side method `#current`.
  1264. The `smalltalk` object holds all class and packages defined in the system.
  1265. ## Classes
  1266. Classes can be accessed using the following methods:
  1267. - `#classes` answers the full list of Smalltalk classes in the system
  1268. - `#at:` answers a specific class of `nil`
  1269. ## Packages
  1270. Packages can be accessed using the following methods:
  1271. - `#packages` answers the full list of packages
  1272. - `#packageAt:` answers a specific class of `nil`
  1273. __note:__ classes and packages are accessed using strings, not symbols
  1274. ## Parsing
  1275. The `#parse:` method is used to parse Amber source code.
  1276. It requires the `Compiler` package and the `js/parser.js` parser file in order to work!
  1277. !Smalltalk methodsFor: 'accessing'!
  1278. at: aString
  1279. <return self[aString]>
  1280. !
  1281. parse: aString
  1282. | result |
  1283. self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
  1284. ^result
  1285. !
  1286. readJSObject: anObject
  1287. <return self.readJSObject(anObject)>
  1288. !
  1289. reservedWords
  1290. "JavaScript reserved words"
  1291. <return self.reservedWords>
  1292. !
  1293. version
  1294. "Answer the version string of Amber"
  1295. ^ '0.10'
  1296. ! !
  1297. !Smalltalk methodsFor: 'classes'!
  1298. classes
  1299. <return self.classes()>
  1300. !
  1301. deleteClass: aClass
  1302. "Deletes a class by deleting its binding only. Use #removeClass instead"
  1303. <self.removeClass(aClass)>
  1304. !
  1305. removeClass: aClass
  1306. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  1307. self deleteClass: aClass.
  1308. SystemAnnouncer current
  1309. announce: (ClassRemoved new
  1310. theClass: aClass;
  1311. yourself)
  1312. ! !
  1313. !Smalltalk methodsFor: 'error handling'!
  1314. asSmalltalkException: anObject
  1315. "A JavaScript exception may be thrown.
  1316. We then need to convert it back to a Smalltalk object"
  1317. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  1318. ifTrue: [ anObject ]
  1319. ifFalse: [ JavaScriptException on: anObject ]
  1320. !
  1321. parseError: anException parsing: aString
  1322. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  1323. ! !
  1324. !Smalltalk methodsFor: 'packages'!
  1325. createPackage: packageName
  1326. "Create and bind a new package with given name and return it."
  1327. <return smalltalk.addPackage(packageName)>
  1328. !
  1329. deletePackage: packageName
  1330. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  1331. To remove a package, use #removePackage instead."
  1332. <delete smalltalk.packages[packageName]>
  1333. !
  1334. packageAt: packageName
  1335. <return self.packages[packageName]>
  1336. !
  1337. packageAt: packageName ifAbsent: aBlock
  1338. ^(self packageAt: packageName) ifNil: aBlock
  1339. !
  1340. packages
  1341. "Return all Package instances in the system."
  1342. <return self.packages.all()>
  1343. !
  1344. pseudoVariableNames
  1345. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  1346. !
  1347. removePackage: packageName
  1348. "Removes a package and all its classes."
  1349. | pkg |
  1350. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1351. pkg classes do: [:each |
  1352. self removeClass: each].
  1353. self deletePackage: packageName
  1354. !
  1355. renamePackage: packageName to: newName
  1356. "Rename a package."
  1357. | pkg |
  1358. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1359. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  1360. (self basicAt: 'packages') at: newName put: pkg.
  1361. pkg name: newName.
  1362. self deletePackage: packageName.
  1363. ! !
  1364. !Smalltalk methodsFor: 'private'!
  1365. basicParse: aString
  1366. <return smalltalk.parser.parse(aString)>
  1367. !
  1368. createPackage: packageName properties: aDict
  1369. "Needed to import .st files: they begin with this call."
  1370. self deprecatedAPI.
  1371. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  1372. ^ self createPackage: packageName
  1373. ! !
  1374. !Smalltalk methodsFor: 'testing'!
  1375. isSmalltalkObject: anObject
  1376. "Consider anObject a Smalltalk object if it has a 'klass' property.
  1377. Note that this may be unaccurate"
  1378. <return typeof anObject.klass !!== 'undefined'>
  1379. ! !
  1380. Smalltalk class instanceVariableNames: 'current'!
  1381. !Smalltalk class methodsFor: 'accessing'!
  1382. current
  1383. <return smalltalk>
  1384. ! !
  1385. Object subclass: #Timeout
  1386. instanceVariableNames: 'rawTimeout'
  1387. package: 'Kernel-Objects'!
  1388. !Timeout commentStamp!
  1389. I am wrapping the returns from `set{Timeout,Interval}`.
  1390. ## Motivation
  1391. Number suffices in browsers, but node.js returns an object.!
  1392. !Timeout methodsFor: 'accessing'!
  1393. rawTimeout: anObject
  1394. rawTimeout := anObject
  1395. ! !
  1396. !Timeout methodsFor: 'timeout/interval'!
  1397. clearInterval
  1398. <
  1399. var interval = self["@rawTimeout"];
  1400. clearInterval(interval);
  1401. >
  1402. !
  1403. clearTimeout
  1404. <
  1405. var timeout = self["@rawTimeout"];
  1406. clearTimeout(timeout);
  1407. >
  1408. ! !
  1409. !Timeout class methodsFor: 'instance creation'!
  1410. on: anObject
  1411. ^self new rawTimeout: anObject; yourself
  1412. ! !
  1413. Object subclass: #UndefinedObject
  1414. instanceVariableNames: ''
  1415. package: 'Kernel-Objects'!
  1416. !UndefinedObject commentStamp!
  1417. 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.
  1418. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  1419. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  1420. !UndefinedObject methodsFor: 'class creation'!
  1421. subclass: aString instanceVariableNames: anotherString
  1422. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1423. !
  1424. subclass: aString instanceVariableNames: aString2 category: aString3
  1425. "Kept for compatibility."
  1426. self deprecatedAPI.
  1427. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1428. !
  1429. subclass: aString instanceVariableNames: aString2 package: aString3
  1430. ^ClassBuilder new
  1431. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  1432. ! !
  1433. !UndefinedObject methodsFor: 'converting'!
  1434. asJSON
  1435. ^null
  1436. ! !
  1437. !UndefinedObject methodsFor: 'copying'!
  1438. deepCopy
  1439. ^self
  1440. !
  1441. shallowCopy
  1442. ^self
  1443. ! !
  1444. !UndefinedObject methodsFor: 'printing'!
  1445. printOn: aStream
  1446. aStream nextPutAll: 'nil'
  1447. ! !
  1448. !UndefinedObject methodsFor: 'testing'!
  1449. ifNil: aBlock
  1450. "inlined in the Compiler"
  1451. ^self ifNil: aBlock ifNotNil: []
  1452. !
  1453. ifNil: aBlock ifNotNil: anotherBlock
  1454. "inlined in the Compiler"
  1455. ^aBlock value
  1456. !
  1457. ifNotNil: aBlock
  1458. "inlined in the Compiler"
  1459. ^self
  1460. !
  1461. ifNotNil: aBlock ifNil: anotherBlock
  1462. "inlined in the Compiler"
  1463. ^anotherBlock value
  1464. !
  1465. isImmutable
  1466. ^ true
  1467. !
  1468. isNil
  1469. ^true
  1470. !
  1471. notNil
  1472. ^false
  1473. ! !
  1474. !UndefinedObject class methodsFor: 'instance creation'!
  1475. new
  1476. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1477. ! !
  1478. !String methodsFor: '*Kernel-Objects'!
  1479. asJavaScriptSelector
  1480. "Return first keyword of the selector, without trailing colon."
  1481. ^self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  1482. ! !