Kernel-Objects.st 28 KB

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