Kernel-Objects.st 38 KB

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