Kernel-Objects.st 38 KB

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