2
0

Kernel-Objects.st 35 KB

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