Kernel-Objects.st 26 KB

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