Kernel-Objects.st 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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 if 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 methodsFor: 'accessing'!
  978. jsObject: aJSObject
  979. jsObject := aJSObject
  980. !
  981. jsObject
  982. ^jsObject
  983. !
  984. at: aSymbol
  985. | attr |
  986. attr := aSymbol asString.
  987. <return self['@jsObject'][attr]>
  988. !
  989. at: aSymbol put: anObject
  990. | attr |
  991. attr := aSymbol asString.
  992. <self['@jsObject'][attr] = anObject>
  993. ! !
  994. !JSObjectProxy methodsFor: 'proxy'!
  995. printString
  996. ^self jsObject toString
  997. !
  998. inspectOn: anInspector
  999. | variables |
  1000. variables := Dictionary new.
  1001. variables at: '#self' put: self jsObject.
  1002. anInspector setLabel: self printString.
  1003. <for(var i in self['@jsObject']) {
  1004. variables._at_put_(i, self['@jsObject'][i]);
  1005. }>.
  1006. anInspector setVariables: variables
  1007. !
  1008. doesNotUnderstand: aMessage
  1009. | obj selector jsSelector arguments |
  1010. obj := self jsObject.
  1011. selector := aMessage selector.
  1012. jsSelector := selector asJavaScriptSelector.
  1013. arguments := aMessage arguments.
  1014. <if(obj[jsSelector] !!= undefined) {return smalltalk.send(obj, jsSelector, arguments)}>.
  1015. super doesNotUnderstand: aMessage
  1016. ! !
  1017. !JSObjectProxy class methodsFor: 'instance creation'!
  1018. on: aJSObject
  1019. ^self new
  1020. jsObject: aJSObject;
  1021. yourself
  1022. ! !