2
0

Kernel-Objects.st 35 KB

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