Kernel-Objects.st 39 KB

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