Kernel-Objects.st 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  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. renameClass: aClass to: aClassName
  584. (Smalltalk current at: aClassName)
  585. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  586. ClassBuilder new renameClass: aClass to: aClassName
  587. ! !
  588. !Environment methodsFor: 'compiling'!
  589. addInstVarNamed: aString to: aClass
  590. self classBuilder
  591. addSubclassOf: aClass superclass
  592. named: aClass name
  593. instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
  594. package: aClass package name
  595. !
  596. compileClassComment: aString for: aClass
  597. aClass comment: aString
  598. !
  599. compileClassDefinition: aString
  600. self eval: aString on: DoIt new
  601. !
  602. compileMethod: sourceCode for: class protocol: protocol
  603. ^ class
  604. compile: sourceCode
  605. category: protocol
  606. ! !
  607. Object subclass: #JSObjectProxy
  608. instanceVariableNames: 'jsObject'
  609. package: 'Kernel-Objects'!
  610. !JSObjectProxy commentStamp!
  611. JSObjectProxy handles sending messages to JavaScript object, therefore accessing JavaScript objects from Amber is transparent.
  612. JSOjbectProxy makes intensive use of `#doesNotUnderstand:`.
  613. ## Examples
  614. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  615. window alert: 'hello world'.
  616. window inspect.
  617. (window jQuery: 'body') append: 'hello world'
  618. 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.
  619. ## Message conversion rules
  620. - `someUser name` becomes `someUser.name`
  621. - `someUser name: 'John'` becomes `someUser name = "John"`
  622. - `console log: 'hello world'` becomes `console.log('hello world')`
  623. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  624. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  625. !JSObjectProxy methodsFor: 'accessing'!
  626. at: aString
  627. <return self['@jsObject'][aString]>
  628. !
  629. at: aString ifAbsent: aBlock
  630. "return the aString property or evaluate aBlock if the property is not defined on the object"
  631. <
  632. var obj = self['@jsObject'];
  633. return aString in obj ? obj[aString] : aBlock();
  634. >
  635. !
  636. at: aString ifPresent: aBlock
  637. "return the evaluation of aBlock with the value if the property is defined or return nil"
  638. <
  639. var obj = self['@jsObject'];
  640. return aString in obj ? aBlock(obj[aString]) : nil;
  641. >
  642. !
  643. at: aString ifPresent: aBlock ifAbsent: anotherBlock
  644. "return the evaluation of aBlock with the value if the property is defined
  645. or return value of anotherBlock"
  646. <
  647. var obj = self['@jsObject'];
  648. return aString in obj ? aBlock(obj[aString]) : anotherBlock();
  649. >
  650. !
  651. at: aString put: anObject
  652. <self['@jsObject'][aString] = anObject>
  653. !
  654. jsObject
  655. ^jsObject
  656. !
  657. jsObject: aJSObject
  658. jsObject := aJSObject
  659. !
  660. lookupProperty: aString
  661. "Looks up a property in JS object.
  662. Answer the property if it is present, or nil if it is not present."
  663. <return aString in self._jsObject() ? aString : nil>
  664. !
  665. value
  666. "if attribute 'value' exists on the JS object return it,
  667. otherwise return the result of Object>>value."
  668. ^ self
  669. at: 'value'
  670. ifAbsent: [ super value ]
  671. ! !
  672. !JSObjectProxy methodsFor: 'enumerating'!
  673. keysAndValuesDo: aBlock
  674. <
  675. var o = self['@jsObject'];
  676. for(var i in o) {
  677. aBlock(i, o[i]);
  678. }
  679. >
  680. ! !
  681. !JSObjectProxy methodsFor: 'printing'!
  682. printOn: aStream
  683. aStream nextPutAll: self jsObject toString
  684. ! !
  685. !JSObjectProxy methodsFor: 'proxy'!
  686. addObjectVariablesTo: aDictionary
  687. <
  688. for(var i in self['@jsObject']) {
  689. aDictionary._at_put_(i, self['@jsObject'][i]);
  690. }
  691. >
  692. !
  693. doesNotUnderstand: aMessage
  694. ^ (self lookupProperty: aMessage selector asJavaScriptSelector)
  695. ifNil: [ super doesNotUnderstand: aMessage ]
  696. ifNotNil: [ :jsSelector |
  697. self
  698. forwardMessage: jsSelector
  699. withArguments: aMessage arguments ]
  700. !
  701. forwardMessage: aString withArguments: anArray
  702. <
  703. return smalltalk.send(self._jsObject(), aString, anArray);
  704. >
  705. !
  706. inspectOn: anInspector
  707. | variables |
  708. variables := Dictionary new.
  709. variables at: '#self' put: self jsObject.
  710. anInspector setLabel: self printString.
  711. self addObjectVariablesTo: variables.
  712. anInspector setVariables: variables
  713. ! !
  714. !JSObjectProxy class methodsFor: 'instance creation'!
  715. on: aJSObject
  716. ^self new
  717. jsObject: aJSObject;
  718. yourself
  719. ! !
  720. Object subclass: #Number
  721. instanceVariableNames: ''
  722. package: 'Kernel-Objects'!
  723. !Number commentStamp!
  724. Number holds the most general methods for dealing with numbers.
  725. Number is directly mapped to JavaScript Number.
  726. Most arithmetic methods like `#+` `#/` `#-` `#max:` are directly inlined into javascript.
  727. ##Enumerating
  728. A Number can be used to evaluate a Block a fixed number of times:
  729. 5 timesRepeat: [Transcript show: 'This will be printed 5 times'; cr].
  730. 1 to: 5 do: [:aNumber| Transcript show: aNumber asString; cr].
  731. 1 to: 10 by: 2 do: [:aNumber| Transcript show: aNumber asString; cr].!
  732. !Number methodsFor: 'accessing'!
  733. identityHash
  734. ^self asString, 'n'
  735. ! !
  736. !Number methodsFor: 'arithmetic'!
  737. * aNumber
  738. "Inlined in the Compiler"
  739. <return self * aNumber>
  740. !
  741. + aNumber
  742. "Inlined in the Compiler"
  743. <return self + aNumber>
  744. !
  745. - aNumber
  746. "Inlined in the Compiler"
  747. <return self - aNumber>
  748. !
  749. / aNumber
  750. "Inlined in the Compiler"
  751. <return self / aNumber>
  752. !
  753. \\ aNumber
  754. <return self % aNumber>
  755. !
  756. abs
  757. <return Math.abs(self);>
  758. !
  759. max: aNumber
  760. <return Math.max(self, aNumber);>
  761. !
  762. min: aNumber
  763. <return Math.min(self, aNumber);>
  764. !
  765. negated
  766. ^0 - self
  767. !
  768. sqrt
  769. <return Math.sqrt(self)>
  770. !
  771. squared
  772. ^self * self
  773. ! !
  774. !Number methodsFor: 'comparing'!
  775. < aNumber
  776. "Inlined in the Compiler"
  777. <return self < aNumber>
  778. !
  779. <= aNumber
  780. "Inlined in the Compiler"
  781. <return self <= aNumber>
  782. !
  783. = aNumber
  784. <
  785. if(!! aNumber._isNumber || !! aNumber._isNumber()) {
  786. return false;
  787. }
  788. return Number(self) == aNumber
  789. >
  790. !
  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. !Number methodsFor: 'converting'!
  800. & aNumber
  801. <return self & aNumber>
  802. !
  803. @ aNumber
  804. ^Point x: self y: aNumber
  805. !
  806. asJSON
  807. ^self
  808. !
  809. asJavascript
  810. ^'(', self printString, ')'
  811. !
  812. asPoint
  813. ^Point x: self y: self
  814. !
  815. asString
  816. < return String(self) >
  817. !
  818. atRandom
  819. ^(Random new next * self) truncated + 1
  820. !
  821. rounded
  822. <return Math.round(self);>
  823. !
  824. to: aNumber
  825. | array first last count |
  826. first := self truncated.
  827. last := aNumber truncated + 1.
  828. count := 1.
  829. array := Array new.
  830. (last - first) timesRepeat: [
  831. array at: count put: first.
  832. count := count + 1.
  833. first := first + 1].
  834. ^array
  835. !
  836. to: stop by: step
  837. | array value pos |
  838. value := self.
  839. array := Array new.
  840. pos := 1.
  841. step = 0 ifTrue: [self error: 'step must be non-zero'].
  842. step < 0
  843. ifTrue: [[ value >= stop ] whileTrue: [
  844. array at: pos put: value.
  845. pos := pos + 1.
  846. value := value + step]]
  847. ifFalse: [[ value <= stop ] whileTrue: [
  848. array at: pos put: value.
  849. pos := pos + 1.
  850. value := value + step]].
  851. ^array
  852. !
  853. truncated
  854. <
  855. if(self >>= 0) {
  856. return Math.floor(self);
  857. } else {
  858. return Math.floor(self * (-1)) * (-1);
  859. };
  860. >
  861. !
  862. | aNumber
  863. <return self | aNumber>
  864. ! !
  865. !Number methodsFor: 'copying'!
  866. copy
  867. ^self
  868. !
  869. deepCopy
  870. ^self copy
  871. ! !
  872. !Number methodsFor: 'enumerating'!
  873. timesRepeat: aBlock
  874. | count |
  875. count := 1.
  876. [count > self] whileFalse: [
  877. aBlock value.
  878. count := count + 1]
  879. !
  880. to: stop by: step do: aBlock
  881. | value |
  882. value := self.
  883. step = 0 ifTrue: [self error: 'step must be non-zero'].
  884. step < 0
  885. ifTrue: [[ value >= stop ] whileTrue: [
  886. aBlock value: value.
  887. value := value + step]]
  888. ifFalse: [[ value <= stop ] whileTrue: [
  889. aBlock value: value.
  890. value := value + step]]
  891. !
  892. to: stop do: aBlock
  893. "Evaluate aBlock for each number from self to aNumber."
  894. | nextValue |
  895. nextValue := self.
  896. [nextValue <= stop]
  897. whileTrue:
  898. [aBlock value: nextValue.
  899. nextValue := nextValue + 1]
  900. ! !
  901. !Number methodsFor: 'printing'!
  902. printOn: aStream
  903. aStream nextPutAll: self asString
  904. !
  905. printShowingDecimalPlaces: placesDesired
  906. <return self.toFixed(placesDesired)>
  907. ! !
  908. !Number methodsFor: 'testing'!
  909. IsImmutable
  910. ^ true
  911. !
  912. even
  913. ^ 0 = (self \\ 2)
  914. !
  915. isNumber
  916. ^true
  917. !
  918. isZero
  919. ^self = 0
  920. !
  921. negative
  922. "Answer whether the receiver is mathematically negative."
  923. ^ self < 0
  924. !
  925. odd
  926. ^ self even not
  927. !
  928. positive
  929. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  930. ^ self >= 0
  931. ! !
  932. !Number class methodsFor: 'instance creation'!
  933. pi
  934. <return Math.PI>
  935. ! !
  936. Object subclass: #Organizer
  937. instanceVariableNames: ''
  938. package: 'Kernel-Objects'!
  939. !Organizer methodsFor: 'accessing'!
  940. addElement: anObject
  941. <self.elements.addElement(anObject)>
  942. !
  943. elements
  944. ^ (self basicAt: 'elements') copy
  945. !
  946. removeElement: anObject
  947. <self.elements.removeElement(anObject)>
  948. ! !
  949. Organizer subclass: #ClassOrganizer
  950. instanceVariableNames: ''
  951. package: 'Kernel-Objects'!
  952. !ClassOrganizer methodsFor: 'accessing'!
  953. addElement: aString
  954. super addElement: aString.
  955. SystemAnnouncer current announce: (ProtocolAdded new
  956. protocol: aString;
  957. theClass: self theClass;
  958. yourself)
  959. !
  960. removeElement: aString
  961. super removeElement: aString.
  962. SystemAnnouncer current announce: (ProtocolRemoved new
  963. protocol: aString;
  964. theClass: self theClass;
  965. yourself)
  966. !
  967. theClass
  968. < return self.theClass >
  969. ! !
  970. Organizer subclass: #PackageOrganizer
  971. instanceVariableNames: ''
  972. package: 'Kernel-Objects'!
  973. Object subclass: #Package
  974. instanceVariableNames: 'commitPathJs commitPathSt'
  975. package: 'Kernel-Objects'!
  976. !Package commentStamp!
  977. 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.
  978. 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.
  979. Packages are manipulated through "Smalltalk current", like for example finding one based on a name:
  980. Smalltalk current packageAt: 'Kernel'
  981. ...but you can also use:
  982. Package named: 'Kernel'
  983. 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
  984. 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
  985. String where the method category "*IDE" defines #inspectOn: which thus is a method belonging to the IDE package.
  986. You can fetch a package from the server:
  987. Package fetch: 'Additional-Examples'!
  988. !Package methodsFor: 'accessing'!
  989. commitPathJs
  990. ^ commitPathJs ifNil: [self class defaultCommitPathJs]
  991. !
  992. commitPathJs: aString
  993. commitPathJs := aString
  994. !
  995. commitPathSt
  996. ^ commitPathSt ifNil: [self class defaultCommitPathSt]
  997. !
  998. commitPathSt: aString
  999. commitPathSt := aString
  1000. !
  1001. name
  1002. <return self.pkgName>
  1003. !
  1004. name: aString
  1005. <self.pkgName = aString>
  1006. !
  1007. organization
  1008. ^ self basicAt: 'organization'
  1009. ! !
  1010. !Package methodsFor: 'classes'!
  1011. classes
  1012. ^ self organization elements
  1013. !
  1014. setupClasses
  1015. self classes
  1016. do: [ :each | ClassBuilder new setupClass: each ];
  1017. do: [ :each | each initialize ]
  1018. !
  1019. sortedClasses
  1020. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  1021. ^self class sortedClasses: self classes
  1022. ! !
  1023. !Package methodsFor: 'printing'!
  1024. printOn: aStream
  1025. super printOn: aStream.
  1026. aStream
  1027. nextPutAll: ' (';
  1028. nextPutAll: self name;
  1029. nextPutAll: ')'
  1030. ! !
  1031. !Package methodsFor: 'testing'!
  1032. isPackage
  1033. ^ true
  1034. ! !
  1035. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  1036. !Package class methodsFor: 'accessing'!
  1037. named: aPackageName
  1038. ^Smalltalk current packageAt: aPackageName
  1039. !
  1040. named: aPackageName ifAbsent: aBlock
  1041. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  1042. ! !
  1043. !Package class methodsFor: 'commit paths'!
  1044. commitPathsFromLoader
  1045. <
  1046. var commitPath = typeof amber !!== 'undefined' && amber.commitPath;
  1047. if (!!commitPath) return;
  1048. if (commitPath.js) self._defaultCommitPathJs_(commitPath.js);
  1049. if (commitPath.st) self._defaultCommitPathSt_(commitPath.st);
  1050. >
  1051. !
  1052. defaultCommitPathJs
  1053. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  1054. !
  1055. defaultCommitPathJs: aString
  1056. defaultCommitPathJs := aString
  1057. !
  1058. defaultCommitPathSt
  1059. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  1060. !
  1061. defaultCommitPathSt: aString
  1062. defaultCommitPathSt := aString
  1063. !
  1064. resetCommitPaths
  1065. defaultCommitPathJs := nil.
  1066. defaultCommitPathSt := nil.
  1067. ! !
  1068. !Package class methodsFor: 'initialization'!
  1069. initialize
  1070. super initialize.
  1071. self commitPathsFromLoader
  1072. ! !
  1073. !Package class methodsFor: 'loading-storing'!
  1074. load: aPackageName
  1075. self load: aPackageName prefix: self defaultCommitPathJs, '/'
  1076. !
  1077. load: aPackageName prefix: aPrefix
  1078. jQuery
  1079. getScript: (aPrefix , aPackageName , '.js')
  1080. onSuccess: [
  1081. (Package named: aPackageName) setupClasses ]
  1082. ! !
  1083. !Package class methodsFor: 'sorting'!
  1084. sortedClasses: classes
  1085. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  1086. | children others nodes expandedClasses |
  1087. children := #().
  1088. others := #().
  1089. classes do: [:each |
  1090. (classes includes: each superclass)
  1091. ifFalse: [children add: each]
  1092. ifTrue: [others add: each]].
  1093. nodes := children collect: [:each |
  1094. ClassSorterNode on: each classes: others level: 0].
  1095. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  1096. expandedClasses := Array new.
  1097. nodes do: [:aNode |
  1098. aNode traverseClassesWith: expandedClasses].
  1099. ^expandedClasses
  1100. ! !
  1101. Object subclass: #Point
  1102. instanceVariableNames: 'x y'
  1103. package: 'Kernel-Objects'!
  1104. !Point commentStamp!
  1105. A `Point` represents an x-y pair of numbers usually designating a geometric coordinate.
  1106. Points are traditionally created using the binary `#@` message to a number:
  1107. 100@120
  1108. Points can then be arithmetically manipulated:
  1109. 100@100 + (10@10)
  1110. ...or for example:
  1111. (100@100) * 2
  1112. **NOTE:** Creating a Point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  1113. 100@ -100 "but 100@-100 would not parse"
  1114. Amber does not have much behavior in this class out-of-the-box.!
  1115. !Point methodsFor: 'accessing'!
  1116. x
  1117. ^x
  1118. !
  1119. x: aNumber
  1120. x := aNumber
  1121. !
  1122. y
  1123. ^y
  1124. !
  1125. y: aNumber
  1126. y := aNumber
  1127. ! !
  1128. !Point methodsFor: 'arithmetic'!
  1129. * aPoint
  1130. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  1131. !
  1132. + aPoint
  1133. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  1134. !
  1135. - aPoint
  1136. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  1137. !
  1138. / aPoint
  1139. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1140. !
  1141. = aPoint
  1142. ^aPoint class = self class and: [
  1143. (aPoint x = self x) & (aPoint y = self y)]
  1144. ! !
  1145. !Point methodsFor: 'converting'!
  1146. asPoint
  1147. ^self
  1148. ! !
  1149. !Point methodsFor: 'printing'!
  1150. printOn: aStream
  1151. "Print receiver in classic x@y notation."
  1152. x printOn: aStream.
  1153. aStream nextPutAll: '@'.
  1154. (y notNil and: [y negative]) ifTrue: [
  1155. "Avoid ambiguous @- construct"
  1156. aStream space ].
  1157. y printOn: aStream
  1158. ! !
  1159. !Point methodsFor: 'transforming'!
  1160. translateBy: delta
  1161. "Answer a Point translated by delta (an instance of Point)."
  1162. ^(delta x + x) @ (delta y + y)
  1163. ! !
  1164. !Point class methodsFor: 'instance creation'!
  1165. x: aNumber y: anotherNumber
  1166. ^self new
  1167. x: aNumber;
  1168. y: anotherNumber;
  1169. yourself
  1170. ! !
  1171. Object subclass: #Random
  1172. instanceVariableNames: ''
  1173. package: 'Kernel-Objects'!
  1174. !Random commentStamp!
  1175. `Random` is a random number generator and is implemented as a trivial wrapper around javascript `Math.random()` and is used like this:
  1176. Random new next
  1177. 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`
  1178. 10 atRandom
  1179. ...and if you want a random number in a specific interval this also works:
  1180. (3 to: 7) atRandom
  1181. ...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:
  1182. 5 atRandom + 2
  1183. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1184. #('a' 'b' 'c') atRandom
  1185. ...or perhaps a letter from a `String`:
  1186. 'abc' atRandom
  1187. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1188. !Random methodsFor: 'accessing'!
  1189. next
  1190. <return Math.random()>
  1191. !
  1192. next: anInteger
  1193. ^(1 to: anInteger) collect: [:each | self next]
  1194. ! !
  1195. Object subclass: #Smalltalk
  1196. instanceVariableNames: ''
  1197. package: 'Kernel-Objects'!
  1198. !Smalltalk commentStamp!
  1199. Smalltalk has only one instance, accessed with `Smalltalk current`.
  1200. It represents the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  1201. The `smalltalk` object holds all class and packages defined in the system.
  1202. ## Classes
  1203. Classes can be accessed using the following methods:
  1204. - `#classes` answers the full list of Smalltalk classes in the system
  1205. - `#at:` answers a specific class of `nil`
  1206. ## Packages
  1207. Packages can be accessed using the following methods:
  1208. - `#packages` answers the full list of packages
  1209. - `#packageAt:` answers a specific class of `nil`
  1210. __note:__ classes and packages are accessed using strings, not symbols
  1211. ## Parsing
  1212. The `#parse:` method is used to parse Smalltalk source code.
  1213. It requires the `Compiler` package and the `js/parser.js` parser file in order to work!
  1214. !Smalltalk methodsFor: 'accessing'!
  1215. at: aString
  1216. <return self[aString]>
  1217. !
  1218. parse: aString
  1219. | result |
  1220. self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
  1221. ^result
  1222. !
  1223. readJSObject: anObject
  1224. <return self.readJSObject(anObject)>
  1225. !
  1226. reservedWords
  1227. "JavaScript reserved words"
  1228. <return self.reservedWords>
  1229. !
  1230. version
  1231. "Answer the version string of Amber"
  1232. ^ '0.10'
  1233. ! !
  1234. !Smalltalk methodsFor: 'classes'!
  1235. classes
  1236. <return self.classes()>
  1237. !
  1238. deleteClass: aClass
  1239. "Deletes a class by deleting its binding only. Use #removeClass instead"
  1240. <self.removeClass(aClass)>
  1241. !
  1242. removeClass: aClass
  1243. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  1244. self deleteClass: aClass.
  1245. SystemAnnouncer current
  1246. announce: (ClassRemoved new
  1247. theClass: aClass;
  1248. yourself)
  1249. ! !
  1250. !Smalltalk methodsFor: 'error handling'!
  1251. asSmalltalkException: anObject
  1252. "A JavaScript exception may be thrown.
  1253. We then need to convert it back to a Smalltalk object"
  1254. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  1255. ifTrue: [ anObject ]
  1256. ifFalse: [ JavaScriptException on: anObject ]
  1257. !
  1258. parseError: anException parsing: aString
  1259. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  1260. ! !
  1261. !Smalltalk methodsFor: 'packages'!
  1262. createPackage: packageName
  1263. "Create and bind a new package with given name and return it."
  1264. <return smalltalk.addPackage(packageName)>
  1265. !
  1266. deletePackage: packageName
  1267. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  1268. To remove a package, use #removePackage instead."
  1269. <delete smalltalk.packages[packageName]>
  1270. !
  1271. packageAt: packageName
  1272. <return self.packages[packageName]>
  1273. !
  1274. packageAt: packageName ifAbsent: aBlock
  1275. ^(self packageAt: packageName) ifNil: aBlock
  1276. !
  1277. packages
  1278. "Return all Package instances in the system."
  1279. <return self.packages.all()>
  1280. !
  1281. pseudoVariableNames
  1282. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  1283. !
  1284. removePackage: packageName
  1285. "Removes a package and all its classes."
  1286. | pkg |
  1287. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1288. pkg classes do: [:each |
  1289. self removeClass: each].
  1290. self deletePackage: packageName
  1291. !
  1292. renamePackage: packageName to: newName
  1293. "Rename a package."
  1294. | pkg |
  1295. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1296. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  1297. (self basicAt: 'packages') at: newName put: pkg.
  1298. pkg name: newName.
  1299. self deletePackage: packageName.
  1300. ! !
  1301. !Smalltalk methodsFor: 'private'!
  1302. basicParse: aString
  1303. <return smalltalk.parser.parse(aString)>
  1304. !
  1305. createPackage: packageName properties: aDict
  1306. "Needed to import .st files: they begin with this call."
  1307. self deprecatedAPI.
  1308. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  1309. ^ self createPackage: packageName
  1310. ! !
  1311. !Smalltalk methodsFor: 'testing'!
  1312. isSmalltalkObject: anObject
  1313. "Consider anObject a Smalltalk object if it has a 'klass' property.
  1314. Note that this may be unaccurate"
  1315. <return typeof anObject.klass !!== 'undefined'>
  1316. ! !
  1317. Smalltalk class instanceVariableNames: 'current'!
  1318. !Smalltalk class methodsFor: 'accessing'!
  1319. current
  1320. <return smalltalk>
  1321. ! !
  1322. Object subclass: #Timeout
  1323. instanceVariableNames: 'rawTimeout'
  1324. package: 'Kernel-Objects'!
  1325. !Timeout commentStamp!
  1326. I am wrapping the returns from set{Timeout,Interval}.
  1327. Number suffices in browsers, but node.js returns an object.!
  1328. !Timeout methodsFor: 'accessing'!
  1329. rawTimeout: anObject
  1330. rawTimeout := anObject
  1331. ! !
  1332. !Timeout methodsFor: 'timeout/interval'!
  1333. clearInterval
  1334. <
  1335. var interval = self["@rawTimeout"];
  1336. clearInterval(interval);
  1337. >
  1338. !
  1339. clearTimeout
  1340. <
  1341. var timeout = self["@rawTimeout"];
  1342. clearTimeout(timeout);
  1343. >
  1344. ! !
  1345. !Timeout class methodsFor: 'instance creation'!
  1346. on: anObject
  1347. ^self new rawTimeout: anObject; yourself
  1348. ! !
  1349. Object subclass: #UndefinedObject
  1350. instanceVariableNames: ''
  1351. package: 'Kernel-Objects'!
  1352. !UndefinedObject commentStamp!
  1353. 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.
  1354. `nil` is the Smalltalk representation of the `undefined` JavaScript object.!
  1355. !UndefinedObject methodsFor: 'class creation'!
  1356. subclass: aString instanceVariableNames: anotherString
  1357. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1358. !
  1359. subclass: aString instanceVariableNames: aString2 category: aString3
  1360. "Kept for compatibility."
  1361. self deprecatedAPI.
  1362. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1363. !
  1364. subclass: aString instanceVariableNames: aString2 package: aString3
  1365. ^ClassBuilder new
  1366. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  1367. ! !
  1368. !UndefinedObject methodsFor: 'converting'!
  1369. asJSON
  1370. ^null
  1371. ! !
  1372. !UndefinedObject methodsFor: 'copying'!
  1373. deepCopy
  1374. ^self
  1375. !
  1376. shallowCopy
  1377. ^self
  1378. ! !
  1379. !UndefinedObject methodsFor: 'printing'!
  1380. printOn: aStream
  1381. aStream nextPutAll: 'nil'
  1382. ! !
  1383. !UndefinedObject methodsFor: 'testing'!
  1384. ifNil: aBlock
  1385. "inlined in the Compiler"
  1386. ^self ifNil: aBlock ifNotNil: []
  1387. !
  1388. ifNil: aBlock ifNotNil: anotherBlock
  1389. "inlined in the Compiler"
  1390. ^aBlock value
  1391. !
  1392. ifNotNil: aBlock
  1393. "inlined in the Compiler"
  1394. ^self
  1395. !
  1396. ifNotNil: aBlock ifNil: anotherBlock
  1397. "inlined in the Compiler"
  1398. ^anotherBlock value
  1399. !
  1400. isImmutable
  1401. ^ true
  1402. !
  1403. isNil
  1404. ^true
  1405. !
  1406. notNil
  1407. ^false
  1408. ! !
  1409. !UndefinedObject class methodsFor: 'instance creation'!
  1410. new
  1411. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1412. ! !
  1413. !String methodsFor: '*Kernel-Objects'!
  1414. asJavaScriptSelector
  1415. "Return first keyword of the selector, without trailing colon."
  1416. ^self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  1417. ! !