2
0

Kernel-Objects.st 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  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: 'commitPathJs commitPathSt'
  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. commitPathJs
  1087. ^ commitPathJs ifNil: [self class defaultCommitPathJs]
  1088. !
  1089. commitPathJs: aString
  1090. commitPathJs := aString
  1091. !
  1092. commitPathSt
  1093. ^ commitPathSt ifNil: [self class defaultCommitPathSt]
  1094. !
  1095. commitPathSt: aString
  1096. commitPathSt := aString
  1097. !
  1098. name
  1099. <return self.pkgName>
  1100. !
  1101. name: aString
  1102. <self.pkgName = aString>
  1103. !
  1104. organization
  1105. ^ self basicAt: 'organization'
  1106. ! !
  1107. !Package methodsFor: 'classes'!
  1108. classes
  1109. ^ self organization elements
  1110. !
  1111. setupClasses
  1112. self classes
  1113. do: [ :each | ClassBuilder new setupClass: each ];
  1114. do: [ :each | each initialize ]
  1115. !
  1116. sortedClasses
  1117. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  1118. ^self class sortedClasses: self classes
  1119. ! !
  1120. !Package methodsFor: 'printing'!
  1121. printOn: aStream
  1122. super printOn: aStream.
  1123. aStream
  1124. nextPutAll: ' (';
  1125. nextPutAll: self name;
  1126. nextPutAll: ')'
  1127. ! !
  1128. !Package methodsFor: 'testing'!
  1129. isPackage
  1130. ^ true
  1131. ! !
  1132. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  1133. !Package class methodsFor: 'accessing'!
  1134. named: aPackageName
  1135. ^Smalltalk current packageAt: aPackageName
  1136. !
  1137. named: aPackageName ifAbsent: aBlock
  1138. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  1139. ! !
  1140. !Package class methodsFor: 'commit paths'!
  1141. commitPathsFromLoader
  1142. <
  1143. var commitPath = typeof amber !!== 'undefined' && amber.commitPath;
  1144. if (!!commitPath) return;
  1145. if (commitPath.js) self._defaultCommitPathJs_(commitPath.js);
  1146. if (commitPath.st) self._defaultCommitPathSt_(commitPath.st);
  1147. >
  1148. !
  1149. defaultCommitPathJs
  1150. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  1151. !
  1152. defaultCommitPathJs: aString
  1153. defaultCommitPathJs := aString
  1154. !
  1155. defaultCommitPathSt
  1156. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  1157. !
  1158. defaultCommitPathSt: aString
  1159. defaultCommitPathSt := aString
  1160. !
  1161. resetCommitPaths
  1162. defaultCommitPathJs := nil.
  1163. defaultCommitPathSt := nil.
  1164. ! !
  1165. !Package class methodsFor: 'initialization'!
  1166. initialize
  1167. super initialize.
  1168. self commitPathsFromLoader
  1169. ! !
  1170. !Package class methodsFor: 'loading-storing'!
  1171. load: aPackageName
  1172. self load: aPackageName prefix: self defaultCommitPathJs, '/'
  1173. !
  1174. load: aPackageName prefix: aPrefix
  1175. jQuery
  1176. getScript: (aPrefix , aPackageName , '.js')
  1177. onSuccess: [
  1178. (Package named: aPackageName) setupClasses ]
  1179. ! !
  1180. !Package class methodsFor: 'sorting'!
  1181. sortedClasses: classes
  1182. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  1183. | children others nodes expandedClasses |
  1184. children := #().
  1185. others := #().
  1186. classes do: [:each |
  1187. (classes includes: each superclass)
  1188. ifFalse: [children add: each]
  1189. ifTrue: [others add: each]].
  1190. nodes := children collect: [:each |
  1191. ClassSorterNode on: each classes: others level: 0].
  1192. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  1193. expandedClasses := Array new.
  1194. nodes do: [:aNode |
  1195. aNode traverseClassesWith: expandedClasses].
  1196. ^expandedClasses
  1197. ! !
  1198. Object subclass: #Point
  1199. instanceVariableNames: 'x y'
  1200. package: 'Kernel-Objects'!
  1201. !Point commentStamp!
  1202. I represent an x-y pair of numbers usually designating a geometric coordinate.
  1203. ## API
  1204. Instances are traditionally created using the binary `#@` message to a number:
  1205. 100@120
  1206. Points can then be arithmetically manipulated:
  1207. 100@100 + (10@10)
  1208. ...or for example:
  1209. (100@100) * 2
  1210. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  1211. 100@ -100 "but 100@-100 would not parse"!
  1212. !Point methodsFor: 'accessing'!
  1213. x
  1214. ^x
  1215. !
  1216. x: aNumber
  1217. x := aNumber
  1218. !
  1219. y
  1220. ^y
  1221. !
  1222. y: aNumber
  1223. y := aNumber
  1224. ! !
  1225. !Point methodsFor: 'arithmetic'!
  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. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1237. !
  1238. = aPoint
  1239. ^aPoint class = self class and: [
  1240. (aPoint x = self x) & (aPoint y = self y)]
  1241. ! !
  1242. !Point methodsFor: 'converting'!
  1243. asPoint
  1244. ^self
  1245. ! !
  1246. !Point methodsFor: 'printing'!
  1247. printOn: aStream
  1248. "Print receiver in classic x@y notation."
  1249. x printOn: aStream.
  1250. aStream nextPutAll: '@'.
  1251. (y notNil and: [y negative]) ifTrue: [
  1252. "Avoid ambiguous @- construct"
  1253. aStream space ].
  1254. y printOn: aStream
  1255. ! !
  1256. !Point methodsFor: 'transforming'!
  1257. translateBy: delta
  1258. "Answer a Point translated by delta (an instance of Point)."
  1259. ^(delta x + x) @ (delta y + y)
  1260. ! !
  1261. !Point class methodsFor: 'helios'!
  1262. heliosClass
  1263. ^ 'magnitude'
  1264. ! !
  1265. !Point class methodsFor: 'instance creation'!
  1266. x: aNumber y: anotherNumber
  1267. ^self new
  1268. x: aNumber;
  1269. y: anotherNumber;
  1270. yourself
  1271. ! !
  1272. Object subclass: #ProgressHandler
  1273. instanceVariableNames: ''
  1274. package: 'Kernel-Objects'!
  1275. !ProgressHandler commentStamp!
  1276. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  1277. Subclasses of can register themselves as the current handler with
  1278. `ProgressHandler class >> register`.
  1279. The default behavior is to simply iterate over the collection.!
  1280. !ProgressHandler methodsFor: 'progress handling'!
  1281. do: aBlock on: aCollection displaying: aString
  1282. aCollection do: aBlock
  1283. ! !
  1284. ProgressHandler class instanceVariableNames: 'current'!
  1285. !ProgressHandler class methodsFor: 'accessing'!
  1286. current
  1287. ^current ifNil: [ current := self new ]
  1288. !
  1289. setCurrent: anHandler
  1290. current := anHandler
  1291. ! !
  1292. !ProgressHandler class methodsFor: 'initialization'!
  1293. initialize
  1294. self register
  1295. !
  1296. register
  1297. ProgressHandler setCurrent: self new
  1298. ! !
  1299. Object subclass: #Random
  1300. instanceVariableNames: ''
  1301. package: 'Kernel-Objects'!
  1302. !Random commentStamp!
  1303. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  1304. ## API
  1305. The typical use case it to use the `#next` method like the following:
  1306. Random new next
  1307. 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`
  1308. 10 atRandom
  1309. A random number in a specific interval can be obtained with the following:
  1310. (3 to: 7) atRandom
  1311. 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:
  1312. 5 atRandom + 2
  1313. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1314. #('a' 'b' 'c') atRandom
  1315. As well as letter from a `String`:
  1316. 'abc' atRandom
  1317. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1318. !Random methodsFor: 'accessing'!
  1319. next
  1320. <return Math.random()>
  1321. !
  1322. next: anInteger
  1323. ^(1 to: anInteger) collect: [:each | self next]
  1324. ! !
  1325. Object subclass: #Smalltalk
  1326. instanceVariableNames: ''
  1327. package: 'Kernel-Objects'!
  1328. !Smalltalk commentStamp!
  1329. I represent the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  1330. ## API
  1331. I have only one instance, accessed with class-side method `#current`.
  1332. The `smalltalk` object holds all class and packages defined in the system.
  1333. ## Classes
  1334. Classes can be accessed using the following methods:
  1335. - `#classes` answers the full list of Smalltalk classes in the system
  1336. - `#at:` answers a specific class or `nil`
  1337. ## Packages
  1338. Packages can be accessed using the following methods:
  1339. - `#packages` answers the full list of packages
  1340. - `#packageAt:` answers a specific package or `nil`
  1341. ## Parsing
  1342. The `#parse:` method is used to parse Amber source code.
  1343. It requires the `Compiler` package and the `js/parser.js` parser file in order to work.!
  1344. !Smalltalk methodsFor: 'accessing'!
  1345. at: aString
  1346. <return self[aString]>
  1347. !
  1348. parse: aString
  1349. | result |
  1350. self
  1351. try: [result := self basicParse: aString]
  1352. catch: [:ex | (self parseError: ex parsing: aString) signal].
  1353. ^ result
  1354. source: aString;
  1355. yourself
  1356. !
  1357. readJSObject: anObject
  1358. <return self.readJSObject(anObject)>
  1359. !
  1360. reservedWords
  1361. "JavaScript reserved words"
  1362. <return self.reservedWords>
  1363. !
  1364. version
  1365. "Answer the version string of Amber"
  1366. ^ '0.10'
  1367. ! !
  1368. !Smalltalk methodsFor: 'classes'!
  1369. classes
  1370. <return self.classes()>
  1371. !
  1372. deleteClass: aClass
  1373. "Deletes a class by deleting its binding only. Use #removeClass instead"
  1374. <self.removeClass(aClass)>
  1375. !
  1376. removeClass: aClass
  1377. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  1378. self deleteClass: aClass.
  1379. SystemAnnouncer current
  1380. announce: (ClassRemoved new
  1381. theClass: aClass;
  1382. yourself)
  1383. ! !
  1384. !Smalltalk methodsFor: 'error handling'!
  1385. asSmalltalkException: anObject
  1386. "A JavaScript exception may be thrown.
  1387. We then need to convert it back to a Smalltalk object"
  1388. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  1389. ifTrue: [ anObject ]
  1390. ifFalse: [ JavaScriptException on: anObject ]
  1391. !
  1392. parseError: anException parsing: aString
  1393. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  1394. ! !
  1395. !Smalltalk methodsFor: 'packages'!
  1396. createPackage: packageName
  1397. "Create and bind a new package with given name and return it."
  1398. <return smalltalk.addPackage(packageName)>
  1399. !
  1400. deletePackage: packageName
  1401. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  1402. To remove a package, use #removePackage instead."
  1403. <delete smalltalk.packages[packageName]>
  1404. !
  1405. packageAt: packageName
  1406. <return self.packages[packageName]>
  1407. !
  1408. packageAt: packageName ifAbsent: aBlock
  1409. ^(self packageAt: packageName) ifNil: aBlock
  1410. !
  1411. packages
  1412. "Return all Package instances in the system."
  1413. <return self.packages.all()>
  1414. !
  1415. pseudoVariableNames
  1416. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  1417. !
  1418. removePackage: packageName
  1419. "Removes a package and all its classes."
  1420. | pkg |
  1421. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1422. pkg classes do: [:each |
  1423. self removeClass: each].
  1424. self deletePackage: packageName
  1425. !
  1426. renamePackage: packageName to: newName
  1427. "Rename a package."
  1428. | pkg |
  1429. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  1430. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  1431. (self basicAt: 'packages') at: newName put: pkg.
  1432. pkg name: newName.
  1433. self deletePackage: packageName.
  1434. ! !
  1435. !Smalltalk methodsFor: 'private'!
  1436. basicParse: aString
  1437. <return smalltalk.parser.parse(aString)>
  1438. !
  1439. createPackage: packageName properties: aDict
  1440. "Needed to import .st files: they begin with this call."
  1441. self deprecatedAPI.
  1442. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  1443. ^ self createPackage: packageName
  1444. ! !
  1445. !Smalltalk methodsFor: 'testing'!
  1446. isSmalltalkObject: anObject
  1447. "Consider anObject a Smalltalk object if it has a 'klass' property.
  1448. Note that this may be unaccurate"
  1449. <return typeof anObject.klass !!== 'undefined'>
  1450. ! !
  1451. Smalltalk class instanceVariableNames: 'current'!
  1452. !Smalltalk class methodsFor: 'accessing'!
  1453. current
  1454. <return smalltalk>
  1455. ! !
  1456. Object subclass: #Timeout
  1457. instanceVariableNames: 'rawTimeout'
  1458. package: 'Kernel-Objects'!
  1459. !Timeout commentStamp!
  1460. I am wrapping the returns from `set{Timeout,Interval}`.
  1461. ## Motivation
  1462. Number suffices in browsers, but node.js returns an object.!
  1463. !Timeout methodsFor: 'accessing'!
  1464. rawTimeout: anObject
  1465. rawTimeout := anObject
  1466. ! !
  1467. !Timeout methodsFor: 'timeout/interval'!
  1468. clearInterval
  1469. <
  1470. var interval = self["@rawTimeout"];
  1471. clearInterval(interval);
  1472. >
  1473. !
  1474. clearTimeout
  1475. <
  1476. var timeout = self["@rawTimeout"];
  1477. clearTimeout(timeout);
  1478. >
  1479. ! !
  1480. !Timeout class methodsFor: 'instance creation'!
  1481. on: anObject
  1482. ^self new rawTimeout: anObject; yourself
  1483. ! !
  1484. Object subclass: #UndefinedObject
  1485. instanceVariableNames: ''
  1486. package: 'Kernel-Objects'!
  1487. !UndefinedObject commentStamp!
  1488. 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.
  1489. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  1490. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  1491. !UndefinedObject methodsFor: 'class creation'!
  1492. subclass: aString instanceVariableNames: anotherString
  1493. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1494. !
  1495. subclass: aString instanceVariableNames: aString2 category: aString3
  1496. "Kept for compatibility."
  1497. self deprecatedAPI.
  1498. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1499. !
  1500. subclass: aString instanceVariableNames: aString2 package: aString3
  1501. ^ClassBuilder new
  1502. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  1503. ! !
  1504. !UndefinedObject methodsFor: 'converting'!
  1505. asJSON
  1506. ^null
  1507. ! !
  1508. !UndefinedObject methodsFor: 'copying'!
  1509. deepCopy
  1510. ^self
  1511. !
  1512. shallowCopy
  1513. ^self
  1514. ! !
  1515. !UndefinedObject methodsFor: 'printing'!
  1516. printOn: aStream
  1517. aStream nextPutAll: 'nil'
  1518. ! !
  1519. !UndefinedObject methodsFor: 'testing'!
  1520. ifNil: aBlock
  1521. "inlined in the Compiler"
  1522. ^self ifNil: aBlock ifNotNil: []
  1523. !
  1524. ifNil: aBlock ifNotNil: anotherBlock
  1525. "inlined in the Compiler"
  1526. ^aBlock value
  1527. !
  1528. ifNotNil: aBlock
  1529. "inlined in the Compiler"
  1530. ^self
  1531. !
  1532. ifNotNil: aBlock ifNil: anotherBlock
  1533. "inlined in the Compiler"
  1534. ^anotherBlock value
  1535. !
  1536. isImmutable
  1537. ^ true
  1538. !
  1539. isNil
  1540. ^true
  1541. !
  1542. notNil
  1543. ^false
  1544. ! !
  1545. !UndefinedObject class methodsFor: 'instance creation'!
  1546. new
  1547. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1548. ! !
  1549. !String methodsFor: '*Kernel-Objects'!
  1550. asJavaScriptSelector
  1551. "Return first keyword of the selector, without trailing colon."
  1552. ^self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  1553. ! !