1
0

Kernel-Objects.st 31 KB

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