1
0

Kernel-Objects.st 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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 commentStamp!
  480. Number holds the most general methods for dealing with numbers.
  481. Number is directly mapped to JavaScript Number.
  482. Most arithmetic methods like `#+` `#/` `#-` `#max:` are directly inlined into javascript.
  483. ##Enumerating
  484. A Number can be used to evaluate a Block a fixed number of times:
  485. 5 timesRepeat: [Transcript show: 'This will be printed 5 times'; cr].
  486. 1 to: 5 do: [:aNumber| Transcript show: aNumber asString; cr].
  487. 1 to: 10 by: 2 do: [:aNumber| Transcript show: aNumber asString; cr].!
  488. !Number methodsFor: ''!
  489. ! !
  490. !Number methodsFor: 'accessing'!
  491. identityHash
  492. ^self asString, 'n'
  493. ! !
  494. !Number methodsFor: 'arithmetic'!
  495. + aNumber
  496. "Inlined in the Compiler"
  497. <return self + aNumber>
  498. !
  499. - aNumber
  500. "Inlined in the Compiler"
  501. <return self - aNumber>
  502. !
  503. * aNumber
  504. "Inlined in the Compiler"
  505. <return self * aNumber>
  506. !
  507. / aNumber
  508. "Inlined in the Compiler"
  509. <return self / aNumber>
  510. !
  511. max: aNumber
  512. <return Math.max(self, aNumber);>
  513. !
  514. min: aNumber
  515. <return Math.min(self, aNumber);>
  516. !
  517. negated
  518. ^0 - self
  519. !
  520. \\ aNumber
  521. <return self % aNumber>
  522. !
  523. sqrt
  524. <return Math.sqrt(self)>
  525. !
  526. squared
  527. ^self * self
  528. ! !
  529. !Number methodsFor: 'comparing'!
  530. = aNumber
  531. aNumber isNumber ifFalse: [^false].
  532. <return Number(self) == aNumber>
  533. !
  534. > aNumber
  535. "Inlined in the Compiler"
  536. <return self >> aNumber>
  537. !
  538. < aNumber
  539. "Inlined in the Compiler"
  540. <return self < aNumber>
  541. !
  542. >= aNumber
  543. "Inlined in the Compiler"
  544. <return self >>= aNumber>
  545. !
  546. <= aNumber
  547. "Inlined in the Compiler"
  548. <return self <= aNumber>
  549. ! !
  550. !Number methodsFor: 'converting'!
  551. rounded
  552. <return Math.round(self);>
  553. !
  554. truncated
  555. |result|
  556. self >= 0
  557. ifTrue: [<result = Math.floor(self);>]
  558. ifFalse: [<result = (Math.floor(self * (-1)) * (-1));>].
  559. ^ result
  560. !
  561. to: aNumber
  562. | array first last count |
  563. first := self truncated.
  564. last := aNumber truncated + 1.
  565. count := 1.
  566. array := Array new.
  567. (last - first) timesRepeat: [
  568. array at: count put: first.
  569. count := count + 1.
  570. first := first + 1].
  571. ^array
  572. !
  573. asString
  574. ^self printString
  575. !
  576. asJavascript
  577. ^'(', self printString, ')'
  578. !
  579. atRandom
  580. ^(Random new next * self) truncated + 1
  581. !
  582. @ aNumber
  583. ^Point x: self y: aNumber
  584. !
  585. asPoint
  586. ^Point x: self y: self
  587. !
  588. to: stop by: step
  589. | array value pos |
  590. value := self.
  591. array := Array new.
  592. pos := 1.
  593. step = 0 ifTrue: [self error: 'step must be non-zero'].
  594. step < 0
  595. ifTrue: [[ value >= stop ] whileTrue: [
  596. array at: pos put: value.
  597. pos := pos + 1.
  598. value := value + step]]
  599. ifFalse: [[ value <= stop ] whileTrue: [
  600. array at: pos put: value.
  601. pos := pos + 1.
  602. value := value + step]].
  603. ^array
  604. ! !
  605. !Number methodsFor: 'copying'!
  606. deepCopy
  607. ^self copy
  608. !
  609. copy
  610. ^self
  611. ! !
  612. !Number methodsFor: 'enumerating'!
  613. timesRepeat: aBlock
  614. | integer count |
  615. integer := self truncated.
  616. count := 1.
  617. [count > self] whileFalse: [
  618. aBlock value.
  619. count := count + 1]
  620. !
  621. to: stop do: aBlock
  622. "Evaluate aBlock for each number from self to aNumber."
  623. | nextValue |
  624. nextValue := self.
  625. [nextValue <= stop]
  626. whileTrue:
  627. [aBlock value: nextValue.
  628. nextValue := nextValue + 1]
  629. !
  630. to: stop by: step do: aBlock
  631. | value |
  632. value := self.
  633. step = 0 ifTrue: [self error: 'step must be non-zero'].
  634. step < 0
  635. ifTrue: [[ value >= stop ] whileTrue: [
  636. aBlock value: value.
  637. value := value + step]]
  638. ifFalse: [[ value <= stop ] whileTrue: [
  639. aBlock value: value.
  640. value := value + step]]
  641. ! !
  642. !Number methodsFor: 'printing'!
  643. printString
  644. <return String(self)>
  645. !
  646. printShowingDecimalPlaces: placesDesired
  647. <return self.toFixed(placesDesired)>
  648. ! !
  649. !Number methodsFor: 'testing'!
  650. isNumber
  651. ^true
  652. !
  653. even
  654. ^ 0 = (self \\ 2)
  655. !
  656. odd
  657. ^ self even not
  658. !
  659. negative
  660. "Answer whether the receiver is mathematically negative."
  661. ^ self < 0
  662. !
  663. positive
  664. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  665. ^ self >= 0
  666. !
  667. isZero
  668. ^self = 0
  669. ! !
  670. !Number methodsFor: 'timeouts/intervals'!
  671. clearInterval
  672. <clearInterval(Number(self))>
  673. !
  674. clearTimeout
  675. <clearTimeout(Number(self))>
  676. ! !
  677. !Number class methodsFor: 'instance creation'!
  678. pi
  679. <return Math.PI>
  680. ! !
  681. Object subclass: #Boolean
  682. instanceVariableNames: ''
  683. category: 'Kernel-Objects'!
  684. !Boolean commentStamp!
  685. Boolean wraps the JavaScript `Boolean()` constructor. The `true` and `false` objects are the JavaScript boolean objects.
  686. Boolean defines the protocol for logic testing operations and conditional control structures for the logical values.
  687. Boolean instances are weither `true` or `false`.!
  688. !Boolean methodsFor: 'comparing'!
  689. = aBoolean
  690. aBoolean class = self class ifFalse: [^false].
  691. <return Boolean(self == true) == aBoolean>
  692. ! !
  693. !Boolean methodsFor: 'controlling'!
  694. ifTrue: aBlock
  695. "inlined in the Compiler"
  696. ^self ifTrue: aBlock ifFalse: []
  697. !
  698. ifFalse: aBlock
  699. "inlined in the Compiler"
  700. ^self ifTrue: [] ifFalse: aBlock
  701. !
  702. ifFalse: aBlock ifTrue: anotherBlock
  703. "inlined in the Compiler"
  704. ^self ifTrue: anotherBlock ifFalse: aBlock
  705. !
  706. ifTrue: aBlock ifFalse: anotherBlock
  707. "inlined in the Compiler"
  708. <
  709. if(self == true) {
  710. return aBlock();
  711. } else {
  712. return anotherBlock();
  713. }
  714. >
  715. !
  716. and: aBlock
  717. ^self = true
  718. ifTrue: aBlock
  719. ifFalse: [false]
  720. !
  721. or: aBlock
  722. ^self = true
  723. ifTrue: [true]
  724. ifFalse: aBlock
  725. !
  726. not
  727. ^self = false
  728. !
  729. & aBoolean
  730. <
  731. if(self == true) {
  732. return aBoolean;
  733. } else {
  734. return false;
  735. }
  736. >
  737. !
  738. | aBoolean
  739. <
  740. if(self == true) {
  741. return true;
  742. } else {
  743. return aBoolean;
  744. }
  745. >
  746. ! !
  747. !Boolean methodsFor: 'copying'!
  748. shallowCopy
  749. ^self
  750. !
  751. deepCopy
  752. ^self
  753. ! !
  754. !Boolean methodsFor: 'printing'!
  755. printString
  756. <return self.toString()>
  757. ! !
  758. Object subclass: #Date
  759. instanceVariableNames: ''
  760. category: 'Kernel-Objects'!
  761. !Date commentStamp!
  762. The Date class is used to work with dates and times. Therefore `Date today` and `Date now` are both valid in
  763. Amber and answer the same date object.
  764. Date wraps the `Date()` JavaScript constructor, and Smalltalk date objects are JavaScript date objects.!
  765. !Date methodsFor: 'accessing'!
  766. year
  767. <return self.getFullYear()>
  768. !
  769. month
  770. <return self.getMonth() + 1>
  771. !
  772. month: aNumber
  773. <self.setMonth(aNumber - 1)>
  774. !
  775. day
  776. ^self dayOfWeek
  777. !
  778. dayOfWeek
  779. <return self.getDay() + 1>
  780. !
  781. dayOfWeek: aNumber
  782. <return self.setDay(aNumber - 1)>
  783. !
  784. day: aNumber
  785. self day: aNumber
  786. !
  787. year: aNumber
  788. <self.setFullYear(aNumber)>
  789. !
  790. dayOfMonth
  791. <return self.getDate()>
  792. !
  793. dayOfMonth: aNumber
  794. <self.setDate(aNumber)>
  795. !
  796. time
  797. <return self.getTime()>
  798. !
  799. time: aNumber
  800. <self.setTime(aNumber)>
  801. !
  802. hours: aNumber
  803. <self.setHours(aNumber)>
  804. !
  805. minutes: aNumber
  806. <self.setMinutes(aNumber)>
  807. !
  808. seconds: aNumber
  809. <self.setSeconds(aNumber)>
  810. !
  811. milliseconds: aNumber
  812. <self.setMilliseconds(aNumber)>
  813. !
  814. hours
  815. <return self.getHours()>
  816. !
  817. minutes
  818. <return self.getMinutes()>
  819. !
  820. seconds
  821. <return self.getSeconds()>
  822. !
  823. milliseconds
  824. <return self.getMilliseconds()>
  825. ! !
  826. !Date methodsFor: 'arithmetic'!
  827. - aDate
  828. <return self - aDate>
  829. !
  830. + aDate
  831. <return self + aDate>
  832. ! !
  833. !Date methodsFor: 'comparing'!
  834. < aDate
  835. <return self < aDate>
  836. !
  837. > aDate
  838. <return self >> aDate>
  839. !
  840. <= aDate
  841. <return self <= aDate>
  842. !
  843. >= aDate
  844. <return self >>= aDate>
  845. ! !
  846. !Date methodsFor: 'converting'!
  847. asString
  848. <return self.toString()>
  849. !
  850. asMilliseconds
  851. ^self time
  852. !
  853. asDateString
  854. <return self.toDateString()>
  855. !
  856. asTimeString
  857. <return self.toTimeString()>
  858. !
  859. asLocaleString
  860. <return self.toLocaleString()>
  861. !
  862. asNumber
  863. ^self asMilliseconds
  864. ! !
  865. !Date methodsFor: 'printing'!
  866. printString
  867. ^self asString
  868. ! !
  869. !Date class methodsFor: 'instance creation'!
  870. new: anObject
  871. <return new Date(anObject)>
  872. !
  873. fromString: aString
  874. "Example: Date fromString('2011/04/15 00:00:00')"
  875. ^self new: aString
  876. !
  877. fromSeconds: aNumber
  878. ^self fromMilliseconds: aNumber * 1000
  879. !
  880. fromMilliseconds: aNumber
  881. ^self new: aNumber
  882. !
  883. today
  884. ^self new
  885. !
  886. now
  887. ^self today
  888. !
  889. millisecondsToRun: aBlock
  890. | t |
  891. t := Date now.
  892. aBlock value.
  893. ^Date now - t
  894. ! !
  895. Object subclass: #UndefinedObject
  896. instanceVariableNames: ''
  897. category: 'Kernel-Objects'!
  898. !UndefinedObject commentStamp!
  899. 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.
  900. `nil` is the Smalltalk representation of the `undefined` JavaScript object.!
  901. !UndefinedObject methodsFor: 'class creation'!
  902. subclass: aString instanceVariableNames: anotherString
  903. ^self subclass: aString instanceVariableNames: anotherString package: nil
  904. !
  905. subclass: aString instanceVariableNames: aString2 category: aString3
  906. "Kept for compatibility."
  907. self deprecatedAPI.
  908. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  909. !
  910. subclass: aString instanceVariableNames: aString2 package: aString3
  911. ^ClassBuilder new
  912. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  913. ! !
  914. !UndefinedObject methodsFor: 'copying'!
  915. shallowCopy
  916. ^self
  917. !
  918. deepCopy
  919. ^self
  920. ! !
  921. !UndefinedObject methodsFor: 'printing'!
  922. printString
  923. ^'nil'
  924. ! !
  925. !UndefinedObject methodsFor: 'testing'!
  926. ifNil: aBlock
  927. "inlined in the Compiler"
  928. ^self ifNil: aBlock ifNotNil: []
  929. !
  930. ifNotNil: aBlock
  931. "inlined in the Compiler"
  932. ^self
  933. !
  934. ifNil: aBlock ifNotNil: anotherBlock
  935. "inlined in the Compiler"
  936. ^aBlock value
  937. !
  938. ifNotNil: aBlock ifNil: anotherBlock
  939. "inlined in the Compiler"
  940. ^anotherBlock value
  941. !
  942. isNil
  943. ^true
  944. !
  945. notNil
  946. ^false
  947. ! !
  948. !UndefinedObject class methodsFor: 'instance creation'!
  949. new
  950. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  951. ! !
  952. Object subclass: #Random
  953. instanceVariableNames: ''
  954. category: 'Kernel-Objects'!
  955. !Random commentStamp!
  956. `Random` is a random number generator and is implemented as a trivial wrapper around javascript `Math.random()` and is used like this:
  957. Random new next
  958. 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`
  959. 10 atRandom
  960. ...and if you want a random number in a specific interval this also works:
  961. (3 to: 7) atRandom
  962. ...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:
  963. 5 atRandom + 2
  964. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  965. #('a' 'b' 'c') atRandom
  966. ...or perhaps a letter from a `String`:
  967. 'abc' atRandom
  968. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  969. !Random methodsFor: 'accessing'!
  970. next
  971. <return Math.random()>
  972. !
  973. next: anInteger
  974. ^(1 to: anInteger) collect: [:each | self next]
  975. ! !
  976. Object subclass: #Point
  977. instanceVariableNames: 'x y'
  978. category: 'Kernel-Objects'!
  979. !Point commentStamp!
  980. A `Point` represents an x-y pair of numbers usually designating a geometric coordinate.
  981. Points are traditionally created using the binary `#@` message to a number:
  982. 100@120
  983. Points can then be arithmetically manipulated:
  984. 100@100 + (10@10)
  985. ...or for example:
  986. (100@100) * 2
  987. **NOTE:** Creating a Point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  988. 100@ -100 "but 100@-100 would not parse"
  989. Amber does not have much behavior in this class out-of-the-box.!
  990. !Point methodsFor: 'accessing'!
  991. x
  992. ^x
  993. !
  994. y
  995. ^y
  996. !
  997. y: aNumber
  998. y := aNumber
  999. !
  1000. x: aNumber
  1001. x := aNumber
  1002. ! !
  1003. !Point methodsFor: 'arithmetic'!
  1004. * aPoint
  1005. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  1006. !
  1007. + aPoint
  1008. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  1009. !
  1010. - aPoint
  1011. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  1012. !
  1013. / aPoint
  1014. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1015. !
  1016. = aPoint
  1017. ^aPoint class = self class and: [
  1018. (aPoint x = self x) & (aPoint y = self y)]
  1019. ! !
  1020. !Point methodsFor: 'converting'!
  1021. asPoint
  1022. ^self
  1023. ! !
  1024. !Point methodsFor: 'printing'!
  1025. printString
  1026. "Print receiver in classic x@y notation."
  1027. ^String streamContents: [:stream |
  1028. stream nextPutAll: x printString, '@'.
  1029. (y notNil and: [y negative])
  1030. ifTrue: [
  1031. "Avoid ambiguous @- construct"
  1032. stream space].
  1033. stream nextPutAll: y printString]
  1034. ! !
  1035. !Point class methodsFor: 'instance creation'!
  1036. x: aNumber y: anotherNumber
  1037. ^self new
  1038. x: aNumber;
  1039. y: anotherNumber;
  1040. yourself
  1041. ! !
  1042. Object subclass: #JSObjectProxy
  1043. instanceVariableNames: 'jsObject'
  1044. category: 'Kernel-Objects'!
  1045. !JSObjectProxy commentStamp!
  1046. JSObjectProxy handles sending messages to JavaScript object, therefore accessing JavaScript objects from Amber is transparent.
  1047. JSOjbectProxy makes intensive use of `#doesNotUnderstand:`.
  1048. ## Examples
  1049. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  1050. window alert: 'hello world'.
  1051. window inspect.
  1052. (window jQuery: 'body') append: 'hello world'
  1053. 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.
  1054. ## Message conversion rules
  1055. - `someUser name` becomes `someUser.name`
  1056. - `someUser name: 'John'` becomes `someUser name = "John"`
  1057. - `console log: 'hello world'` becomes `console.log('hello world')`
  1058. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  1059. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  1060. !JSObjectProxy methodsFor: 'accessing'!
  1061. jsObject: aJSObject
  1062. jsObject := aJSObject
  1063. !
  1064. jsObject
  1065. ^jsObject
  1066. !
  1067. at: aSymbol
  1068. | attr |
  1069. attr := aSymbol asString.
  1070. <return self['@jsObject'][attr]>
  1071. !
  1072. at: aSymbol put: anObject
  1073. | attr |
  1074. attr := aSymbol asString.
  1075. <self['@jsObject'][attr] = anObject>
  1076. ! !
  1077. !JSObjectProxy methodsFor: 'proxy'!
  1078. printString
  1079. ^self jsObject toString
  1080. !
  1081. inspectOn: anInspector
  1082. | variables |
  1083. variables := Dictionary new.
  1084. variables at: '#self' put: self jsObject.
  1085. anInspector setLabel: self printString.
  1086. <for(var i in self['@jsObject']) {
  1087. variables._at_put_(i, self['@jsObject'][i]);
  1088. }>.
  1089. anInspector setVariables: variables
  1090. !
  1091. doesNotUnderstand: aMessage
  1092. | obj selector jsSelector arguments |
  1093. obj := self jsObject.
  1094. selector := aMessage selector.
  1095. jsSelector := selector asJavaScriptSelector.
  1096. arguments := aMessage arguments.
  1097. <if(obj[jsSelector] !!= undefined) {return smalltalk.send(obj, jsSelector, arguments)}>.
  1098. super doesNotUnderstand: aMessage
  1099. ! !
  1100. !JSObjectProxy class methodsFor: 'instance creation'!
  1101. on: aJSObject
  1102. ^self new
  1103. jsObject: aJSObject;
  1104. yourself
  1105. ! !