Kernel-Objects.st 28 KB

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