2
0

Kernel-Objects.st 35 KB

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