Kernel-Objects.st 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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. !Number methodsFor: 'timeouts/intervals'!
  635. clearInterval
  636. <clearInterval(Number(self))>
  637. !
  638. clearTimeout
  639. <clearTimeout(Number(self))>
  640. ! !
  641. !Number class methodsFor: 'instance creation'!
  642. pi
  643. <return Math.PI>
  644. ! !
  645. Object subclass: #Boolean
  646. instanceVariableNames: ''
  647. category: 'Kernel-Objects'!
  648. !Boolean commentStamp!
  649. Boolean wraps the JavaScript `Boolean()` constructor. The `true` and `false` objects are the JavaScript boolean objects.
  650. Boolean defines the protocol for logic testing operations and conditional control structures for the logical values.
  651. Boolean instances are weither `true` or `false`.!
  652. !Boolean methodsFor: 'comparing'!
  653. = aBoolean
  654. aBoolean class = self class ifFalse: [^false].
  655. <return Boolean(self == true) == aBoolean>
  656. ! !
  657. !Boolean methodsFor: 'controlling'!
  658. ifTrue: aBlock
  659. "inlined in the Compiler"
  660. ^self ifTrue: aBlock ifFalse: []
  661. !
  662. ifFalse: aBlock
  663. "inlined in the Compiler"
  664. ^self ifTrue: [] ifFalse: aBlock
  665. !
  666. ifFalse: aBlock ifTrue: anotherBlock
  667. "inlined in the Compiler"
  668. ^self ifTrue: anotherBlock ifFalse: aBlock
  669. !
  670. ifTrue: aBlock ifFalse: anotherBlock
  671. "inlined in the Compiler"
  672. <
  673. if(self == true) {
  674. return aBlock();
  675. } else {
  676. return anotherBlock();
  677. }
  678. >
  679. !
  680. and: aBlock
  681. ^self = true
  682. ifTrue: aBlock
  683. ifFalse: [false]
  684. !
  685. or: aBlock
  686. ^self = true
  687. ifTrue: [true]
  688. ifFalse: aBlock
  689. !
  690. not
  691. ^self = false
  692. !
  693. & aBoolean
  694. <
  695. if(self == true) {
  696. return aBoolean;
  697. } else {
  698. return false;
  699. }
  700. >
  701. !
  702. | aBoolean
  703. <
  704. if(self == true) {
  705. return true;
  706. } else {
  707. return aBoolean;
  708. }
  709. >
  710. ! !
  711. !Boolean methodsFor: 'copying'!
  712. shallowCopy
  713. ^self
  714. !
  715. deepCopy
  716. ^self
  717. ! !
  718. !Boolean methodsFor: 'printing'!
  719. printString
  720. <return self.toString()>
  721. ! !
  722. Object subclass: #Date
  723. instanceVariableNames: ''
  724. category: 'Kernel-Objects'!
  725. !Date commentStamp!
  726. The Date class is used to work with dates and times. Therefore `Date today` and `Date now` are both valid in
  727. Amber and answer the same date object.
  728. Date wraps the `Date()` JavaScript constructor, and Smalltalk date objects are JavaScript date objects.!
  729. !Date methodsFor: 'accessing'!
  730. year
  731. <return self.getFullYear()>
  732. !
  733. month
  734. <return self.getMonth() + 1>
  735. !
  736. month: aNumber
  737. <self.setMonth(aNumber - 1)>
  738. !
  739. day
  740. ^self dayOfWeek
  741. !
  742. dayOfWeek
  743. <return self.getDay() + 1>
  744. !
  745. dayOfWeek: aNumber
  746. <return self.setDay(aNumber - 1)>
  747. !
  748. day: aNumber
  749. self day: aNumber
  750. !
  751. year: aNumber
  752. <self.setFullYear(aNumber)>
  753. !
  754. dayOfMonth
  755. <return self.getDate()>
  756. !
  757. dayOfMonth: aNumber
  758. <self.setDate(aNumber)>
  759. !
  760. time
  761. <return self.getTime()>
  762. !
  763. time: aNumber
  764. <self.setTime(aNumber)>
  765. !
  766. hours: aNumber
  767. <self.setHours(aNumber)>
  768. !
  769. minutes: aNumber
  770. <self.setMinutes(aNumber)>
  771. !
  772. seconds: aNumber
  773. <self.setSeconds(aNumber)>
  774. !
  775. milliseconds: aNumber
  776. <self.setMilliseconds(aNumber)>
  777. !
  778. hours
  779. <return self.getHours()>
  780. !
  781. minutes
  782. <return self.getMinutes()>
  783. !
  784. seconds
  785. <return self.getSeconds()>
  786. !
  787. milliseconds
  788. <return self.getMilliseconds()>
  789. ! !
  790. !Date methodsFor: 'arithmetic'!
  791. - aDate
  792. <return self - aDate>
  793. !
  794. + aDate
  795. <return self + aDate>
  796. ! !
  797. !Date methodsFor: 'comparing'!
  798. < aDate
  799. <return self < aDate>
  800. !
  801. > aDate
  802. <return self >> aDate>
  803. !
  804. <= aDate
  805. <return self <= aDate>
  806. !
  807. >= aDate
  808. <return self >>= aDate>
  809. ! !
  810. !Date methodsFor: 'converting'!
  811. asString
  812. <return self.toString()>
  813. !
  814. asMilliseconds
  815. ^self time
  816. !
  817. asDateString
  818. <return self.toDateString()>
  819. !
  820. asTimeString
  821. <return self.toTimeString()>
  822. !
  823. asLocaleString
  824. <return self.toLocaleString()>
  825. !
  826. asNumber
  827. ^self asMilliseconds
  828. ! !
  829. !Date methodsFor: 'printing'!
  830. printString
  831. ^self asString
  832. ! !
  833. !Date class methodsFor: 'instance creation'!
  834. new: anObject
  835. <return new Date(anObject)>
  836. !
  837. fromString: aString
  838. "Example: Date fromString('2011/04/15 00:00:00')"
  839. ^self new: aString
  840. !
  841. fromSeconds: aNumber
  842. ^self fromMilliseconds: aNumber * 1000
  843. !
  844. fromMilliseconds: aNumber
  845. ^self new: aNumber
  846. !
  847. today
  848. ^self new
  849. !
  850. now
  851. ^self today
  852. !
  853. millisecondsToRun: aBlock
  854. | t |
  855. t := Date now.
  856. aBlock value.
  857. ^Date now - t
  858. ! !
  859. Object subclass: #UndefinedObject
  860. instanceVariableNames: ''
  861. category: 'Kernel-Objects'!
  862. !UndefinedObject methodsFor: 'class creation'!
  863. subclass: aString instanceVariableNames: anotherString
  864. ^self subclass: aString instanceVariableNames: anotherString package: nil
  865. !
  866. subclass: aString instanceVariableNames: aString2 category: aString3
  867. "Kept for compatibility."
  868. self deprecatedAPI.
  869. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  870. !
  871. subclass: aString instanceVariableNames: aString2 package: aString3
  872. ^ClassBuilder new
  873. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  874. ! !
  875. !UndefinedObject methodsFor: 'copying'!
  876. shallowCopy
  877. ^self
  878. !
  879. deepCopy
  880. ^self
  881. ! !
  882. !UndefinedObject methodsFor: 'printing'!
  883. printString
  884. ^'nil'
  885. ! !
  886. !UndefinedObject methodsFor: 'testing'!
  887. ifNil: aBlock
  888. "inlined in the Compiler"
  889. ^self ifNil: aBlock ifNotNil: []
  890. !
  891. ifNotNil: aBlock
  892. "inlined in the Compiler"
  893. ^self
  894. !
  895. ifNil: aBlock ifNotNil: anotherBlock
  896. "inlined in the Compiler"
  897. ^aBlock value
  898. !
  899. ifNotNil: aBlock ifNil: anotherBlock
  900. "inlined in the Compiler"
  901. ^anotherBlock value
  902. !
  903. isNil
  904. ^true
  905. !
  906. notNil
  907. ^false
  908. ! !
  909. !UndefinedObject class methodsFor: 'instance creation'!
  910. new
  911. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  912. ! !
  913. Object subclass: #Random
  914. instanceVariableNames: ''
  915. category: 'Kernel-Objects'!
  916. !Random commentStamp!
  917. Random is just a wrapper around javascript Math.random() and is trivially used like this:
  918. Random new next
  919. This will return a float x where x < 1 and x > 0. If you want a random integer between 1 and 10 you can use #atRandom
  920. 10 atRandom
  921. ...which is also implemented in SequencableCollection so you can easy pick an element at random:
  922. #('a' 'b' 'c') atRandom!
  923. !Random methodsFor: 'accessing'!
  924. next
  925. <return Math.random()>
  926. !
  927. next: anInteger
  928. ^(1 to: anInteger) collect: [:each | self next]
  929. ! !
  930. Object subclass: #Point
  931. instanceVariableNames: 'x y'
  932. category: 'Kernel-Objects'!
  933. !Point methodsFor: 'accessing'!
  934. x
  935. ^x
  936. !
  937. y
  938. ^y
  939. !
  940. y: aNumber
  941. y := aNumber
  942. !
  943. x: aNumber
  944. x := aNumber
  945. ! !
  946. !Point methodsFor: 'arithmetic'!
  947. * aPoint
  948. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  949. !
  950. + aPoint
  951. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  952. !
  953. - aPoint
  954. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  955. !
  956. / aPoint
  957. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  958. !
  959. = aPoint
  960. ^aPoint class = self class and: [
  961. (aPoint x = self x) & (aPoint y = self y)]
  962. ! !
  963. !Point methodsFor: 'converting'!
  964. asPoint
  965. ^self
  966. ! !
  967. !Point class methodsFor: 'instance creation'!
  968. x: aNumber y: anotherNumber
  969. ^self new
  970. x: aNumber;
  971. y: anotherNumber;
  972. yourself
  973. ! !
  974. Object subclass: #JSObjectProxy
  975. instanceVariableNames: 'jsObject'
  976. category: 'Kernel-Objects'!
  977. !JSObjectProxy commentStamp!
  978. JSObjectProxy handles sending messages to JavaScript object, therefore accessing JavaScript objects from Amber is transparent.
  979. JSOjbectProxy makes intensive use of `#doesNotUnderstand:`.
  980. ## Examples
  981. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  982. window alert: 'hello world'.
  983. window inspect.
  984. (window jQuery: 'body') append: 'hello world'
  985. 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.
  986. ## Message conversion rules
  987. - `someUser name` becomes `someUser.name`
  988. - `someUser name: 'John'` becomes `someUser name = "John"`
  989. - `console log: 'hello world'` becomes `console.log('hello world')`
  990. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  991. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  992. !JSObjectProxy methodsFor: 'accessing'!
  993. jsObject: aJSObject
  994. jsObject := aJSObject
  995. !
  996. jsObject
  997. ^jsObject
  998. !
  999. at: aSymbol
  1000. | attr |
  1001. attr := aSymbol asString.
  1002. <return self['@jsObject'][attr]>
  1003. !
  1004. at: aSymbol put: anObject
  1005. | attr |
  1006. attr := aSymbol asString.
  1007. <self['@jsObject'][attr] = anObject>
  1008. ! !
  1009. !JSObjectProxy methodsFor: 'proxy'!
  1010. printString
  1011. ^self jsObject toString
  1012. !
  1013. inspectOn: anInspector
  1014. | variables |
  1015. variables := Dictionary new.
  1016. variables at: '#self' put: self jsObject.
  1017. anInspector setLabel: self printString.
  1018. <for(var i in self['@jsObject']) {
  1019. variables._at_put_(i, self['@jsObject'][i]);
  1020. }>.
  1021. anInspector setVariables: variables
  1022. !
  1023. doesNotUnderstand: aMessage
  1024. | obj selector jsSelector arguments |
  1025. obj := self jsObject.
  1026. selector := aMessage selector.
  1027. jsSelector := selector asJavaScriptSelector.
  1028. arguments := aMessage arguments.
  1029. <if(obj[jsSelector] !!= undefined) {return smalltalk.send(obj, jsSelector, arguments)}>.
  1030. super doesNotUnderstand: aMessage
  1031. ! !
  1032. !JSObjectProxy class methodsFor: 'instance creation'!
  1033. on: aJSObject
  1034. ^self new
  1035. jsObject: aJSObject;
  1036. yourself
  1037. ! !