Kernel-Objects.st 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. Smalltalk current createPackage: 'Kernel-Objects' properties: #{}!
  2. nil subclass: #Object
  3. instanceVariableNames: ''
  4. category: '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. yourself
  31. ^self
  32. !
  33. class
  34. <return self.klass>
  35. !
  36. size
  37. self error: 'Object not indexable'
  38. !
  39. instVarAt: aSymbol
  40. | varname |
  41. varname := aSymbol asString.
  42. <return self['@'+varname]>
  43. !
  44. instVarAt: aSymbol put: anObject
  45. | varname |
  46. varname := aSymbol asString.
  47. <self['@' + varname] = anObject>
  48. !
  49. basicAt: aString
  50. <return self[aString]>
  51. !
  52. basicAt: aString put: anObject
  53. <return self[aString] = anObject>
  54. !
  55. basicDelete: aString
  56. <delete self[aString]; return aString>
  57. !
  58. identityHash
  59. <return self.identityHash || (self.identityHash = smalltalk.nextId());>
  60. ! !
  61. !Object methodsFor: 'comparing'!
  62. = anObject
  63. ^self == anObject
  64. !
  65. ~= anObject
  66. ^(self = anObject) = false
  67. !
  68. == anObject
  69. ^self identityHash = anObject identityHash
  70. !
  71. ~~ anObject
  72. ^(self == anObject) = false
  73. ! !
  74. !Object methodsFor: 'converting'!
  75. -> anObject
  76. ^Association key: self value: anObject
  77. !
  78. asString
  79. ^self printString
  80. !
  81. asJavascript
  82. ^self asString
  83. !
  84. asJSON
  85. ^JSON parse: self asJSONString
  86. !
  87. asJSONString
  88. ^JSON stringify: self
  89. ! !
  90. !Object methodsFor: 'copying'!
  91. copy
  92. ^self shallowCopy postCopy
  93. !
  94. shallowCopy
  95. <
  96. var copy = self.klass._new();
  97. for(var i in self) {
  98. if(/^@.+/.test(i)) {
  99. copy[i] = self[i];
  100. }
  101. }
  102. return copy;
  103. >
  104. !
  105. deepCopy
  106. <
  107. var copy = self.klass._new();
  108. for(var i in self) {
  109. if(/^@.+/.test(i)) {
  110. copy[i] = self[i]._deepCopy();
  111. }
  112. }
  113. return copy;
  114. >
  115. !
  116. postCopy
  117. ! !
  118. !Object methodsFor: 'error handling'!
  119. error: aString
  120. Error signal: aString
  121. !
  122. subclassResponsibility
  123. self error: 'This method is a responsibility of a subclass'
  124. !
  125. shouldNotImplement
  126. self error: 'This method should not be implemented in ', self class name
  127. !
  128. try: aBlock catch: anotherBlock
  129. <try{result = aBlock()} catch(e) {result = anotherBlock(e)};
  130. return result;>
  131. !
  132. doesNotUnderstand: aMessage
  133. MessageNotUnderstood new
  134. receiver: self;
  135. message: aMessage;
  136. signal
  137. !
  138. halt
  139. self error: 'Halt encountered'
  140. !
  141. deprecatedAPI
  142. "Just a simple way to deprecate methods.
  143. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  144. but it could in the future."
  145. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'
  146. ! !
  147. !Object methodsFor: 'initialization'!
  148. initialize
  149. ! !
  150. !Object methodsFor: 'message handling'!
  151. perform: aSymbol
  152. ^self perform: aSymbol withArguments: #()
  153. !
  154. perform: aSymbol withArguments: aCollection
  155. ^self basicPerform: aSymbol asSelector withArguments: aCollection
  156. !
  157. basicPerform: aSymbol
  158. ^self basicPerform: aSymbol withArguments: #()
  159. !
  160. basicPerform: aSymbol withArguments: aCollection
  161. <return self[aSymbol].apply(self, aCollection);>
  162. ! !
  163. !Object methodsFor: 'printing'!
  164. printString
  165. ^'a ', self class name
  166. !
  167. printNl
  168. <console.log(self)>
  169. !
  170. log: aString block: aBlock
  171. | result |
  172. console log: aString, ' time: ', (Date millisecondsToRun: [result := aBlock value]) printString.
  173. ^result
  174. !
  175. storeString
  176. "Answer a String representation of the receiver from which the receiver
  177. can be reconstructed."
  178. ^ String streamContents: [:s | self storeOn: s]
  179. !
  180. storeOn: aStream
  181. aStream nextPutAll: self printString
  182. ! !
  183. !Object methodsFor: 'testing'!
  184. isKindOf: aClass
  185. ^(self isMemberOf: aClass)
  186. ifTrue: [true]
  187. ifFalse: [self class inheritsFrom: aClass]
  188. !
  189. isMemberOf: aClass
  190. ^self class = aClass
  191. !
  192. ifNil: aBlock
  193. "inlined in the Compiler"
  194. ^self
  195. !
  196. ifNil: aBlock ifNotNil: anotherBlock
  197. "inlined in the Compiler"
  198. ^anotherBlock value
  199. !
  200. ifNotNil: aBlock
  201. "inlined in the Compiler"
  202. ^aBlock value
  203. !
  204. ifNotNil: aBlock ifNil: anotherBlock
  205. "inlined in the Compiler"
  206. ^aBlock value
  207. !
  208. isNil
  209. ^false
  210. !
  211. notNil
  212. ^self isNil not
  213. !
  214. isClass
  215. ^false
  216. !
  217. isMetaclass
  218. ^false
  219. !
  220. isNumber
  221. ^false
  222. !
  223. isString
  224. ^false
  225. !
  226. isParseFailure
  227. ^false
  228. !
  229. isSymbol
  230. ^false
  231. ! !
  232. !Object class methodsFor: 'initialization'!
  233. initialize
  234. "no op"
  235. ! !
  236. Object subclass: #Smalltalk
  237. instanceVariableNames: ''
  238. category: 'Kernel-Objects'!
  239. !Smalltalk commentStamp!
  240. Smalltalk has only one instance, accessed with `Smalltalk current`.
  241. It represents the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  242. The `smalltalk` object holds all class and packages defined in the system.
  243. ## Classes
  244. Classes can be accessed using the following methods:
  245. - `#classes` answers the full list of Smalltalk classes in the system
  246. - `#at:` answers a specific class of `nil`
  247. ## Packages
  248. Packages can be accessed using the following methods:
  249. - `#packages` answers the full list of packages
  250. - `#packageAt:` answers a specific class of `nil`
  251. __note:__ classes and packages are accessed using strings, not symbols
  252. ## Parsing
  253. The `#parse:` method is used to parse Smalltalk source code.
  254. It requires the `Compiler` package and the `js/parser.js` parser file in order to work!
  255. !Smalltalk methodsFor: 'accessing'!
  256. classes
  257. <return self.classes()>
  258. !
  259. at: aString
  260. <return self[aString]>
  261. !
  262. basicParse: aString
  263. <return smalltalk.parser.parse(aString)>
  264. !
  265. parse: aString
  266. | result |
  267. self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
  268. ^result
  269. !
  270. parseError: anException parsing: aString
  271. | row col message lines badLine code |
  272. <row = anException.line;
  273. col = anException.column;
  274. message = anException.message;>.
  275. lines := aString lines.
  276. badLine := lines at: row.
  277. badLine := (badLine copyFrom: 1 to: col - 1), ' ===>', (badLine copyFrom: col to: badLine size).
  278. lines at: row put: badLine.
  279. code := String streamContents: [:s |
  280. lines withIndexDo: [:l :i |
  281. s nextPutAll: i asString, ': ', l, String lf]].
  282. ^ Error new messageText: ('Parse error on line ' , row , ' column ' , col , ' : ' , message , ' Below is code with line numbers and ===> marker inserted:' , String lf, code)
  283. !
  284. reservedWords
  285. "JavaScript reserved words"
  286. <return self.reservedWords>
  287. !
  288. readJSObject: anObject
  289. <return self.readJSObject(anObject)>
  290. ! !
  291. !Smalltalk methodsFor: 'classes'!
  292. removeClass: aClass
  293. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  294. aClass methodDictionary values do: [:each |
  295. aClass removeCompiledMethod: each].
  296. aClass class methodDictionary values do: [:each |
  297. aClass class removeCompiledMethod: each].
  298. self basicDelete: aClass name
  299. ! !
  300. !Smalltalk methodsFor: 'packages'!
  301. packages
  302. "Return all Package instances in the system."
  303. <return self.packages.all()>
  304. !
  305. packageAt: packageName
  306. <return self.packages[packageName]>
  307. !
  308. packageAt: packageName ifAbsent: aBlock
  309. ^(self packageAt: packageName) ifNil: aBlock
  310. !
  311. removePackage: packageName
  312. "Removes a package and all its classes."
  313. | pkg |
  314. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  315. pkg classes do: [:each |
  316. self removeClass: each].
  317. self deletePackage: packageName
  318. !
  319. renamePackage: packageName to: newName
  320. "Rename a package."
  321. | pkg |
  322. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  323. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  324. <smalltalk.packages[newName] = smalltalk.packages[packageName]>.
  325. pkg name: newName.
  326. self deletePackage: packageName.
  327. ! !
  328. !Smalltalk methodsFor: 'private'!
  329. createPackage: packageName
  330. "Create and bind a new package with given name and return it."
  331. <return smalltalk.addPackage(packageName, nil)>
  332. !
  333. deletePackage: packageName
  334. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  335. To remove a package, use #removePackage instead."
  336. <delete smalltalk.packages[packageName]>
  337. !
  338. createPackage: packageName properties: aDict
  339. "Create and bind a new package with given name and return it."
  340. | object |
  341. <object = {};>.
  342. aDict keysAndValuesDo: [:key :value |
  343. <object[key] = value>.
  344. ].
  345. <return smalltalk.addPackage(packageName, object)>
  346. ! !
  347. Smalltalk class instanceVariableNames: 'current'!
  348. !Smalltalk class methodsFor: 'accessing'!
  349. current
  350. <return smalltalk>
  351. ! !
  352. Object subclass: #Package
  353. instanceVariableNames: 'commitPathJs commitPathSt'
  354. category: 'Kernel-Objects'!
  355. !Package commentStamp!
  356. 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.
  357. 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.
  358. Packages are manipulated through "Smalltalk current", like for example finding one based on a name:
  359. Smalltalk current packageAt: 'Kernel'
  360. ...but you can also use:
  361. Package named: 'Kernel'
  362. 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
  363. 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
  364. String where the method category "*IDE" defines #inspectOn: which thus is a method belonging to the IDE package.
  365. You can fetch a package from the server:
  366. Package fetch: 'Additional-Examples'!
  367. !Package methodsFor: 'accessing'!
  368. name
  369. <return self.pkgName>
  370. !
  371. name: aString
  372. <self.pkgName = aString>
  373. !
  374. dependencies
  375. ^self propertyAt: 'dependencies' ifAbsent: [#()]
  376. !
  377. dependencies: anArray
  378. ^self propertyAt: 'dependencies' put: anArray
  379. !
  380. properties
  381. ^Smalltalk current readJSObject: (self basicAt: 'properties')
  382. !
  383. properties: aDict
  384. "We store it as a javascript object."
  385. | object |
  386. <object = {};>.
  387. aDict keysAndValuesDo: [:key :value |
  388. <object[key] = value>.
  389. ].
  390. <return self.properties = object>
  391. !
  392. commitPathJs
  393. ^ commitPathJs ifNil: [self class defaultCommitPathJs]
  394. !
  395. commitPathJs: aString
  396. commitPathJs := aString
  397. !
  398. commitPathSt
  399. ^ commitPathSt ifNil: [self class defaultCommitPathSt]
  400. !
  401. commitPathSt: aString
  402. commitPathSt := aString
  403. ! !
  404. !Package methodsFor: 'classes'!
  405. classes
  406. "We need to do a reverse scan."
  407. ^Smalltalk current classes select: [:c | c package == self]
  408. ! !
  409. !Package methodsFor: 'printing'!
  410. printString
  411. ^self name
  412. ! !
  413. !Package methodsFor: 'private'!
  414. propertiesAsJSON
  415. <return JSON.stringify(self.properties)>
  416. !
  417. jsProperties
  418. <return self.properties>
  419. !
  420. jsProperties: aJSObject
  421. <return self.properties = aJSObject>
  422. ! !
  423. !Package methodsFor: 'properties'!
  424. propertyAt: key
  425. <return self.properties[key]>
  426. !
  427. propertyAt: key put: value
  428. <return self.properties[key] = value>
  429. !
  430. propertyAt: key ifAbsent: block
  431. ^(self propertyAt: key) ifNil: [block value]
  432. ! !
  433. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  434. !Package class methodsFor: 'commit paths'!
  435. defaultCommitPathJs
  436. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  437. !
  438. defaultCommitPathJs: aString
  439. defaultCommitPathJs := aString
  440. !
  441. defaultCommitPathSt
  442. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  443. !
  444. defaultCommitPathSt: aString
  445. defaultCommitPathSt := aString
  446. !
  447. resetCommitPaths
  448. defaultCommitPathJs := nil.
  449. defaultCommitPathSt := nil.
  450. ! !
  451. !Package class methodsFor: 'loading-storing'!
  452. fetch: aPackageName prefix: aPrefix
  453. jQuery getScript: (aPrefix , aPackageName , '.js') onSuccess: [ Package init: aPackageName ]
  454. !
  455. fetch: aPackageName
  456. self fetch: aPackageName prefix: self defaultCommitPathJs, '/'
  457. !
  458. commitToLocalStorage: aPackageName
  459. | key sourceCode |
  460. key := 'smalltalk.packages.' , aPackageName.
  461. sourceCode := Exporter new exportPackage: aPackageName.
  462. <localStorage[key] = escape(sourceCode)>
  463. !
  464. init: aPackageName
  465. (smalltalk classes select: [ :each | <each.pkg.pkgName == aPackageName> ])
  466. do: [ :each | <smalltalk.init(each)> ];
  467. do: [ :each | each initialize ]
  468. ! !
  469. !Package class methodsFor: 'not yet classified'!
  470. named: aPackageName
  471. ^Smalltalk current packageAt: aPackageName
  472. !
  473. named: aPackageName ifAbsent: aBlock
  474. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  475. ! !
  476. Object subclass: #Number
  477. instanceVariableNames: ''
  478. category: 'Kernel-Objects'!
  479. !Number methodsFor: ''!
  480. ! !
  481. !Number methodsFor: 'accessing'!
  482. identityHash
  483. ^self asString, 'n'
  484. ! !
  485. !Number methodsFor: 'arithmetic'!
  486. + aNumber
  487. "Inlined in the Compiler"
  488. <return self + aNumber>
  489. !
  490. - aNumber
  491. "Inlined in the Compiler"
  492. <return self - aNumber>
  493. !
  494. * aNumber
  495. "Inlined in the Compiler"
  496. <return self * aNumber>
  497. !
  498. / aNumber
  499. "Inlined in the Compiler"
  500. <return self / aNumber>
  501. !
  502. max: aNumber
  503. <return Math.max(self, aNumber);>
  504. !
  505. min: aNumber
  506. <return Math.min(self, aNumber);>
  507. !
  508. negated
  509. ^0 - self
  510. !
  511. \\ aNumber
  512. <return self % aNumber>
  513. !
  514. sqrt
  515. <return Math.sqrt(self)>
  516. !
  517. squared
  518. ^self * self
  519. ! !
  520. !Number methodsFor: 'comparing'!
  521. = aNumber
  522. aNumber isNumber ifFalse: [^false].
  523. <return Number(self) == aNumber>
  524. !
  525. > aNumber
  526. "Inlined in the Compiler"
  527. <return self >> aNumber>
  528. !
  529. < aNumber
  530. "Inlined in the Compiler"
  531. <return self < aNumber>
  532. !
  533. >= aNumber
  534. "Inlined in the Compiler"
  535. <return self >>= aNumber>
  536. !
  537. <= aNumber
  538. "Inlined in the Compiler"
  539. <return self <= aNumber>
  540. ! !
  541. !Number methodsFor: 'converting'!
  542. rounded
  543. <return Math.round(self);>
  544. !
  545. truncated
  546. |result|
  547. self >= 0
  548. ifTrue: [<result = Math.floor(self);>]
  549. ifFalse: [<result = (Math.floor(self * (-1)) * (-1));>].
  550. ^ result
  551. !
  552. to: aNumber
  553. | array first last count |
  554. first := self truncated.
  555. last := aNumber truncated + 1.
  556. count := 1.
  557. array := Array new.
  558. (last - first) timesRepeat: [
  559. array at: count put: first.
  560. count := count + 1.
  561. first := first + 1].
  562. ^array
  563. !
  564. asString
  565. ^self printString
  566. !
  567. asJavascript
  568. ^'(', self printString, ')'
  569. !
  570. atRandom
  571. ^(Random new next * self) truncated + 1
  572. !
  573. @ aNumber
  574. ^Point x: self y: aNumber
  575. !
  576. asPoint
  577. ^Point x: self y: self
  578. !
  579. to: stop by: step
  580. | array value pos |
  581. value := self.
  582. array := Array new.
  583. pos := 1.
  584. step = 0 ifTrue: [self error: 'step must be non-zero'].
  585. step < 0
  586. ifTrue: [[ value >= stop ] whileTrue: [
  587. array at: pos put: value.
  588. pos := pos + 1.
  589. value := value + step]]
  590. ifFalse: [[ value <= stop ] whileTrue: [
  591. array at: pos put: value.
  592. pos := pos + 1.
  593. value := value + step]].
  594. ^array
  595. ! !
  596. !Number methodsFor: 'copying'!
  597. deepCopy
  598. ^self copy
  599. !
  600. copy
  601. ^self
  602. ! !
  603. !Number methodsFor: 'enumerating'!
  604. timesRepeat: aBlock
  605. | integer count |
  606. integer := self truncated.
  607. count := 1.
  608. [count > self] whileFalse: [
  609. aBlock value.
  610. count := count + 1]
  611. !
  612. to: stop do: aBlock
  613. "Evaluate aBlock for each number from self to aNumber."
  614. | nextValue |
  615. nextValue := self.
  616. [nextValue <= stop]
  617. whileTrue:
  618. [aBlock value: nextValue.
  619. nextValue := nextValue + 1]
  620. !
  621. to: stop by: step do: aBlock
  622. | value |
  623. value := self.
  624. step = 0 ifTrue: [self error: 'step must be non-zero'].
  625. step < 0
  626. ifTrue: [[ value >= stop ] whileTrue: [
  627. aBlock value: value.
  628. value := value + step]]
  629. ifFalse: [[ value <= stop ] whileTrue: [
  630. aBlock value: value.
  631. value := value + step]]
  632. ! !
  633. !Number methodsFor: 'printing'!
  634. printString
  635. <return String(self)>
  636. !
  637. printShowingDecimalPlaces: placesDesired
  638. <return self.toFixed(placesDesired)>
  639. ! !
  640. !Number methodsFor: 'testing'!
  641. isNumber
  642. ^true
  643. !
  644. even
  645. ^ 0 = (self \\ 2)
  646. !
  647. odd
  648. ^ self even not
  649. !
  650. negative
  651. "Answer whether the receiver is mathematically negative."
  652. ^ self < 0
  653. !
  654. positive
  655. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  656. ^ self >= 0
  657. !
  658. isZero
  659. ^self = 0
  660. ! !
  661. !Number methodsFor: 'timeouts/intervals'!
  662. clearInterval
  663. <clearInterval(Number(self))>
  664. !
  665. clearTimeout
  666. <clearTimeout(Number(self))>
  667. ! !
  668. !Number class methodsFor: 'instance creation'!
  669. pi
  670. <return Math.PI>
  671. ! !
  672. Object subclass: #Boolean
  673. instanceVariableNames: ''
  674. category: 'Kernel-Objects'!
  675. !Boolean commentStamp!
  676. Boolean wraps the JavaScript `Boolean()` constructor. The `true` and `false` objects are the JavaScript boolean objects.
  677. Boolean defines the protocol for logic testing operations and conditional control structures for the logical values.
  678. Boolean instances are weither `true` or `false`.!
  679. !Boolean methodsFor: 'comparing'!
  680. = aBoolean
  681. aBoolean class = self class ifFalse: [^false].
  682. <return Boolean(self == true) == aBoolean>
  683. ! !
  684. !Boolean methodsFor: 'controlling'!
  685. ifTrue: aBlock
  686. "inlined in the Compiler"
  687. ^self ifTrue: aBlock ifFalse: []
  688. !
  689. ifFalse: aBlock
  690. "inlined in the Compiler"
  691. ^self ifTrue: [] ifFalse: aBlock
  692. !
  693. ifFalse: aBlock ifTrue: anotherBlock
  694. "inlined in the Compiler"
  695. ^self ifTrue: anotherBlock ifFalse: aBlock
  696. !
  697. ifTrue: aBlock ifFalse: anotherBlock
  698. "inlined in the Compiler"
  699. <
  700. if(self == true) {
  701. return aBlock();
  702. } else {
  703. return anotherBlock();
  704. }
  705. >
  706. !
  707. and: aBlock
  708. ^self = true
  709. ifTrue: aBlock
  710. ifFalse: [false]
  711. !
  712. or: aBlock
  713. ^self = true
  714. ifTrue: [true]
  715. ifFalse: aBlock
  716. !
  717. not
  718. ^self = false
  719. !
  720. & aBoolean
  721. <
  722. if(self == true) {
  723. return aBoolean;
  724. } else {
  725. return false;
  726. }
  727. >
  728. !
  729. | aBoolean
  730. <
  731. if(self == true) {
  732. return true;
  733. } else {
  734. return aBoolean;
  735. }
  736. >
  737. ! !
  738. !Boolean methodsFor: 'copying'!
  739. shallowCopy
  740. ^self
  741. !
  742. deepCopy
  743. ^self
  744. ! !
  745. !Boolean methodsFor: 'printing'!
  746. printString
  747. <return self.toString()>
  748. ! !
  749. Object subclass: #Date
  750. instanceVariableNames: ''
  751. category: 'Kernel-Objects'!
  752. !Date commentStamp!
  753. The Date class is used to work with dates and times. Therefore `Date today` and `Date now` are both valid in
  754. Amber and answer the same date object.
  755. Date wraps the `Date()` JavaScript constructor, and Smalltalk date objects are JavaScript date objects.!
  756. !Date methodsFor: 'accessing'!
  757. year
  758. <return self.getFullYear()>
  759. !
  760. month
  761. <return self.getMonth() + 1>
  762. !
  763. month: aNumber
  764. <self.setMonth(aNumber - 1)>
  765. !
  766. day
  767. ^self dayOfWeek
  768. !
  769. dayOfWeek
  770. <return self.getDay() + 1>
  771. !
  772. dayOfWeek: aNumber
  773. <return self.setDay(aNumber - 1)>
  774. !
  775. day: aNumber
  776. self day: aNumber
  777. !
  778. year: aNumber
  779. <self.setFullYear(aNumber)>
  780. !
  781. dayOfMonth
  782. <return self.getDate()>
  783. !
  784. dayOfMonth: aNumber
  785. <self.setDate(aNumber)>
  786. !
  787. time
  788. <return self.getTime()>
  789. !
  790. time: aNumber
  791. <self.setTime(aNumber)>
  792. !
  793. hours: aNumber
  794. <self.setHours(aNumber)>
  795. !
  796. minutes: aNumber
  797. <self.setMinutes(aNumber)>
  798. !
  799. seconds: aNumber
  800. <self.setSeconds(aNumber)>
  801. !
  802. milliseconds: aNumber
  803. <self.setMilliseconds(aNumber)>
  804. !
  805. hours
  806. <return self.getHours()>
  807. !
  808. minutes
  809. <return self.getMinutes()>
  810. !
  811. seconds
  812. <return self.getSeconds()>
  813. !
  814. milliseconds
  815. <return self.getMilliseconds()>
  816. ! !
  817. !Date methodsFor: 'arithmetic'!
  818. - aDate
  819. <return self - aDate>
  820. !
  821. + aDate
  822. <return self + aDate>
  823. ! !
  824. !Date methodsFor: 'comparing'!
  825. < aDate
  826. <return self < aDate>
  827. !
  828. > aDate
  829. <return self >> aDate>
  830. !
  831. <= aDate
  832. <return self <= aDate>
  833. !
  834. >= aDate
  835. <return self >>= aDate>
  836. ! !
  837. !Date methodsFor: 'converting'!
  838. asString
  839. <return self.toString()>
  840. !
  841. asMilliseconds
  842. ^self time
  843. !
  844. asDateString
  845. <return self.toDateString()>
  846. !
  847. asTimeString
  848. <return self.toTimeString()>
  849. !
  850. asLocaleString
  851. <return self.toLocaleString()>
  852. !
  853. asNumber
  854. ^self asMilliseconds
  855. ! !
  856. !Date methodsFor: 'printing'!
  857. printString
  858. ^self asString
  859. ! !
  860. !Date class methodsFor: 'instance creation'!
  861. new: anObject
  862. <return new Date(anObject)>
  863. !
  864. fromString: aString
  865. "Example: Date fromString('2011/04/15 00:00:00')"
  866. ^self new: aString
  867. !
  868. fromSeconds: aNumber
  869. ^self fromMilliseconds: aNumber * 1000
  870. !
  871. fromMilliseconds: aNumber
  872. ^self new: aNumber
  873. !
  874. today
  875. ^self new
  876. !
  877. now
  878. ^self today
  879. !
  880. millisecondsToRun: aBlock
  881. | t |
  882. t := Date now.
  883. aBlock value.
  884. ^Date now - t
  885. ! !
  886. Object subclass: #UndefinedObject
  887. instanceVariableNames: ''
  888. category: 'Kernel-Objects'!
  889. !UndefinedObject commentStamp!
  890. 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.
  891. `nil` is the Smalltalk representation of the `undefined` JavaScript object.!
  892. !UndefinedObject methodsFor: 'class creation'!
  893. subclass: aString instanceVariableNames: anotherString
  894. ^self subclass: aString instanceVariableNames: anotherString package: nil
  895. !
  896. subclass: aString instanceVariableNames: aString2 category: aString3
  897. "Kept for compatibility."
  898. self deprecatedAPI.
  899. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  900. !
  901. subclass: aString instanceVariableNames: aString2 package: aString3
  902. ^ClassBuilder new
  903. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  904. ! !
  905. !UndefinedObject methodsFor: 'copying'!
  906. shallowCopy
  907. ^self
  908. !
  909. deepCopy
  910. ^self
  911. ! !
  912. !UndefinedObject methodsFor: 'printing'!
  913. printString
  914. ^'nil'
  915. ! !
  916. !UndefinedObject methodsFor: 'testing'!
  917. ifNil: aBlock
  918. "inlined in the Compiler"
  919. ^self ifNil: aBlock ifNotNil: []
  920. !
  921. ifNotNil: aBlock
  922. "inlined in the Compiler"
  923. ^self
  924. !
  925. ifNil: aBlock ifNotNil: anotherBlock
  926. "inlined in the Compiler"
  927. ^aBlock value
  928. !
  929. ifNotNil: aBlock ifNil: anotherBlock
  930. "inlined in the Compiler"
  931. ^anotherBlock value
  932. !
  933. isNil
  934. ^true
  935. !
  936. notNil
  937. ^false
  938. ! !
  939. !UndefinedObject class methodsFor: 'instance creation'!
  940. new
  941. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  942. ! !
  943. Object subclass: #Random
  944. instanceVariableNames: ''
  945. category: 'Kernel-Objects'!
  946. !Random commentStamp!
  947. Random is a random number generator and is implemented as a wrapper around javascript Math.random() and is trivially used like this:
  948. Random new next
  949. 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
  950. 10 atRandom
  951. ...and if you want a random number in a specific interval this also works:
  952. (3 to: 7) atRandom
  953. ...but be aware that #to: does not create an Interval as in other Smalltalks but in fact an Array of numbers, so it's better to use:
  954. 5 atRandom + 2
  955. Since #atRandom is implemented in SequencableCollection you can easy pick an element at random:
  956. #('a' 'b' 'c') atRandom
  957. ...or perhaps a letter from a String:
  958. 'abc' atRandom!
  959. !Random methodsFor: 'accessing'!
  960. next
  961. <return Math.random()>
  962. !
  963. next: anInteger
  964. ^(1 to: anInteger) collect: [:each | self next]
  965. ! !
  966. Object subclass: #Point
  967. instanceVariableNames: 'x y'
  968. category: 'Kernel-Objects'!
  969. !Point commentStamp!
  970. A Point represents an x-y pair of numbers usually designating a geometric coordinate.
  971. Points are traditionally created using the binary #@ message to a number:
  972. 100@120
  973. Points can then be arithmetically manipulated using math:
  974. 100@100 + (10@10)
  975. Creating a Point with a negative y-value will need a space after @ in order to avoid a parsing error,
  976. 10@ -5 works fine, but not 10@-5
  977. Amber does not have much behavior in this class out-of-the-box.!
  978. !Point methodsFor: 'accessing'!
  979. x
  980. ^x
  981. !
  982. y
  983. ^y
  984. !
  985. y: aNumber
  986. y := aNumber
  987. !
  988. x: aNumber
  989. x := aNumber
  990. ! !
  991. !Point methodsFor: 'arithmetic'!
  992. * aPoint
  993. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  994. !
  995. + aPoint
  996. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  997. !
  998. - aPoint
  999. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  1000. !
  1001. / aPoint
  1002. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1003. !
  1004. = aPoint
  1005. ^aPoint class = self class and: [
  1006. (aPoint x = self x) & (aPoint y = self y)]
  1007. ! !
  1008. !Point methodsFor: 'converting'!
  1009. asPoint
  1010. ^self
  1011. ! !
  1012. !Point methodsFor: 'printing'!
  1013. printString
  1014. "Print receiver in classic x@y notation."
  1015. ^String streamContents: [:stream |
  1016. stream nextPutAll: x printString, '@'.
  1017. (y notNil and: [y negative])
  1018. ifTrue: [
  1019. "Avoid ambiguous @- construct"
  1020. stream space].
  1021. stream nextPutAll: y printString]
  1022. ! !
  1023. !Point class methodsFor: 'instance creation'!
  1024. x: aNumber y: anotherNumber
  1025. ^self new
  1026. x: aNumber;
  1027. y: anotherNumber;
  1028. yourself
  1029. ! !
  1030. Object subclass: #JSObjectProxy
  1031. instanceVariableNames: 'jsObject'
  1032. category: 'Kernel-Objects'!
  1033. !JSObjectProxy commentStamp!
  1034. JSObjectProxy handles sending messages to JavaScript object, therefore accessing JavaScript objects from Amber is transparent.
  1035. JSOjbectProxy makes intensive use of `#doesNotUnderstand:`.
  1036. ## Examples
  1037. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  1038. window alert: 'hello world'.
  1039. window inspect.
  1040. (window jQuery: 'body') append: 'hello world'
  1041. 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.
  1042. ## Message conversion rules
  1043. - `someUser name` becomes `someUser.name`
  1044. - `someUser name: 'John'` becomes `someUser name = "John"`
  1045. - `console log: 'hello world'` becomes `console.log('hello world')`
  1046. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  1047. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  1048. !JSObjectProxy methodsFor: 'accessing'!
  1049. jsObject: aJSObject
  1050. jsObject := aJSObject
  1051. !
  1052. jsObject
  1053. ^jsObject
  1054. !
  1055. at: aSymbol
  1056. | attr |
  1057. attr := aSymbol asString.
  1058. <return self['@jsObject'][attr]>
  1059. !
  1060. at: aSymbol put: anObject
  1061. | attr |
  1062. attr := aSymbol asString.
  1063. <self['@jsObject'][attr] = anObject>
  1064. ! !
  1065. !JSObjectProxy methodsFor: 'proxy'!
  1066. printString
  1067. ^self jsObject toString
  1068. !
  1069. inspectOn: anInspector
  1070. | variables |
  1071. variables := Dictionary new.
  1072. variables at: '#self' put: self jsObject.
  1073. anInspector setLabel: self printString.
  1074. <for(var i in self['@jsObject']) {
  1075. variables._at_put_(i, self['@jsObject'][i]);
  1076. }>.
  1077. anInspector setVariables: variables
  1078. !
  1079. doesNotUnderstand: aMessage
  1080. | obj selector jsSelector arguments |
  1081. obj := self jsObject.
  1082. selector := aMessage selector.
  1083. jsSelector := selector asJavaScriptSelector.
  1084. arguments := aMessage arguments.
  1085. <if(obj[jsSelector] !!= undefined) {return smalltalk.send(obj, jsSelector, arguments)}>.
  1086. super doesNotUnderstand: aMessage
  1087. ! !
  1088. !JSObjectProxy class methodsFor: 'instance creation'!
  1089. on: aJSObject
  1090. ^self new
  1091. jsObject: aJSObject;
  1092. yourself
  1093. ! !