Kernel-Objects.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. Smalltalk createPackage: 'Kernel-Objects'!
  2. nil subclass: #ProtoObject
  3. slots: {}
  4. package: 'Kernel-Objects'!
  5. !ProtoObject commentStamp!
  6. I implement the basic behavior required for any object in Amber.
  7. In most cases, subclassing `ProtoObject` is wrong and `Object` should be used instead. However subclassing `ProtoObject` can be useful in some special cases like proxy implementations.!
  8. !ProtoObject methodsFor: 'accessing'!
  9. class
  10. <inlineJS: 'return $self.a$cls'>
  11. !
  12. identityHash
  13. <inlineJS: '
  14. self._deprecatedAPI();
  15. var hash=self.identityHash;
  16. if (hash) return hash;
  17. hash=$core.nextId();
  18. Object.defineProperty(self, ''identityHash'', {value:hash});
  19. return hash;
  20. '>
  21. !
  22. instVarAt: aString
  23. <inlineJS: 'return $self[aString]'>
  24. !
  25. instVarAt: aString put: anObject
  26. <inlineJS: '$self[aString] = anObject'>
  27. !
  28. yourself
  29. ^ self
  30. ! !
  31. !ProtoObject methodsFor: 'comparing'!
  32. = anObject
  33. ^ self == anObject
  34. !
  35. == anObject
  36. <inlineJS: 'return self === anObject'>
  37. !
  38. ~= anObject
  39. ^ (self = anObject) = false
  40. !
  41. ~~ anObject
  42. ^ (self == anObject) = false
  43. ! !
  44. !ProtoObject methodsFor: 'converting'!
  45. asString
  46. ^ self printString
  47. ! !
  48. !ProtoObject methodsFor: 'error handling'!
  49. doesNotUnderstand: aMessage
  50. MessageNotUnderstood new
  51. receiver: self;
  52. message: aMessage;
  53. signal
  54. ! !
  55. !ProtoObject methodsFor: 'evaluating'!
  56. evaluate: aString on: anEvaluator
  57. ^ anEvaluator evaluate: aString receiver: self
  58. ! !
  59. !ProtoObject methodsFor: 'initialization'!
  60. initialize
  61. ! !
  62. !ProtoObject methodsFor: 'inspecting'!
  63. inspect
  64. Inspector inspect: self
  65. !
  66. inspectOn: anInspector
  67. ! !
  68. !ProtoObject methodsFor: 'message handling'!
  69. perform: aString
  70. ^ self perform: aString withArguments: #()
  71. !
  72. perform: aString withArguments: aCollection
  73. <inlineJS: 'return $core.send2(self, aString, aCollection)'>
  74. ! !
  75. !ProtoObject methodsFor: 'printing'!
  76. printOn: aStream
  77. aStream nextPutAll: (self class name first isVowel
  78. ifTrue: [ 'an ' ]
  79. ifFalse: [ 'a ' ]).
  80. aStream nextPutAll: self class name
  81. !
  82. printString
  83. ^ String streamContents: [ :str |
  84. self printOn: str ]
  85. ! !
  86. !ProtoObject methodsFor: 'testing'!
  87. ifNil: aBlock
  88. "inlined in the Compiler"
  89. ^ self
  90. !
  91. ifNil: aBlock ifNotNil: anotherBlock
  92. "inlined in the Compiler"
  93. ^ anotherBlock value: self
  94. !
  95. ifNotNil: aBlock
  96. "inlined in the Compiler"
  97. ^ aBlock value: self
  98. !
  99. ifNotNil: aBlock ifNil: anotherBlock
  100. "inlined in the Compiler"
  101. ^ aBlock value: self
  102. !
  103. isKindOf: aClass
  104. ^ (self isMemberOf: aClass)
  105. ifTrue: [ true ]
  106. ifFalse: [ self class inheritsFrom: aClass ]
  107. !
  108. isNil
  109. ^ false
  110. !
  111. notNil
  112. ^ true
  113. ! !
  114. !ProtoObject class methodsFor: 'initialization'!
  115. initialize
  116. ! !
  117. ProtoObject subclass: #Object
  118. slots: {}
  119. package: 'Kernel-Objects'!
  120. !Object commentStamp!
  121. **I am the root of the Smalltalk class system**. With the exception of unual subclasses of `ProtoObject`, all other classes in the system are subclasses of me.
  122. I provide default behavior common to all normal objects (some of it inherited from `ProtoObject`), such as:
  123. - accessing
  124. - copying
  125. - comparison
  126. - error handling
  127. - message sending
  128. - reflection
  129. Also utility messages that all objects should respond to are defined here.
  130. I have no instance variable.
  131. ##Access
  132. Instance variables can be accessed with `#instVarAt:` and `#instVarAt:put:`. `#instanceVariableNames` answers a collection of all instance variable names.
  133. Accessing JavaScript properties of an object is done through `#basicAt:`, `#basicAt:put:` and `basicDelete:`.
  134. ##Copying
  135. 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.
  136. 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.
  137. ##Comparison
  138. I understand equality `#=` and identity `#==` comparison.
  139. ##Error handling
  140. - `#halt` is the typical message to use for inserting breakpoints during debugging.
  141. - `#error:` throws a generic error exception
  142. - `#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.
  143. Overriding this message can be useful to implement proxies for example.!
  144. !Object methodsFor: 'accessing'!
  145. basicAt: aString
  146. <inlineJS: 'return self[aString]'>
  147. !
  148. basicAt: aString put: anObject
  149. <inlineJS: 'return self[aString] = anObject'>
  150. !
  151. basicDelete: aString
  152. <inlineJS: 'delete self[aString]; return aString'>
  153. !
  154. size
  155. self error: 'Object not indexable'
  156. ! !
  157. !Object methodsFor: 'browsing'!
  158. browse
  159. Finder findClass: self class
  160. ! !
  161. !Object methodsFor: 'converting'!
  162. -> anObject
  163. ^ Association key: self value: anObject
  164. !
  165. asJSONString
  166. ^ JSON stringify: self asJavaScriptObject
  167. !
  168. asJavaScriptObject
  169. | variables |
  170. variables := HashedCollection new.
  171. self class allInstanceVariableNames do: [ :each |
  172. variables at: each put: (self instVarAt: each) asJavaScriptObject ].
  173. ^ variables
  174. !
  175. asJavaScriptSource
  176. ^ self asString
  177. ! !
  178. !Object methodsFor: 'copying'!
  179. copy
  180. ^ self shallowCopy postCopy
  181. !
  182. deepCopy
  183. <inlineJS: '
  184. var copy = self.a$cls._new();
  185. Object.keys(self).forEach(function (i) {
  186. copy[i] = $recv(self[i])._deepCopy();
  187. });
  188. return copy;
  189. '>
  190. !
  191. postCopy
  192. !
  193. shallowCopy
  194. <inlineJS: '
  195. var copy = self.a$cls._new();
  196. Object.keys(self).forEach(function(i) {
  197. copy[i] = self[i];
  198. });
  199. return copy;
  200. '>
  201. ! !
  202. !Object methodsFor: 'error handling'!
  203. deprecatedAPI
  204. "Just a simple way to deprecate methods.
  205. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  206. but it could in the future."
  207. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'.
  208. !
  209. deprecatedAPI: aString
  210. "Just a simple way to deprecate methods.
  211. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  212. but it could in the future."
  213. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'.
  214. console warn: aString
  215. !
  216. error: aString
  217. Error signal: aString
  218. !
  219. halt
  220. Halt signal
  221. !
  222. shouldNotImplement
  223. self error: 'This method should not be implemented in ', self class name
  224. !
  225. subclassResponsibility
  226. self error: 'This method is a responsibility of a subclass'
  227. ! !
  228. !Object methodsFor: 'evaluating'!
  229. in: aValuable
  230. ^ aValuable value: self
  231. !
  232. value
  233. <inlineJS: 'return self.valueOf()'>
  234. ! !
  235. !Object methodsFor: 'message handling'!
  236. basicPerform: aString
  237. ^ self basicPerform: aString withArguments: #()
  238. !
  239. basicPerform: aString withArguments: aCollection
  240. <inlineJS: 'return self[aString].apply(self, aCollection);'>
  241. ! !
  242. !Object methodsFor: 'streaming'!
  243. putOn: aStream
  244. aStream nextPut: self
  245. ! !
  246. !Object methodsFor: 'testing'!
  247. isBehavior
  248. ^ false
  249. !
  250. isBoolean
  251. ^ false
  252. !
  253. isClass
  254. ^ false
  255. !
  256. isCompiledMethod
  257. ^ false
  258. !
  259. isImmutable
  260. ^ false
  261. !
  262. isMemberOf: aClass
  263. ^ self class = aClass
  264. !
  265. isMetaclass
  266. ^ false
  267. !
  268. isNumber
  269. ^ false
  270. !
  271. isPackage
  272. ^ false
  273. !
  274. isParseFailure
  275. ^ false
  276. !
  277. isString
  278. ^ false
  279. !
  280. isSymbol
  281. ^ false
  282. !
  283. respondsTo: aSelector
  284. ^ self class canUnderstand: aSelector
  285. ! !
  286. !Object class methodsFor: 'helios'!
  287. accessorProtocolWith: aGenerator
  288. aGenerator accessorProtocolForObject
  289. !
  290. accessorsSourceCodesWith: aGenerator
  291. aGenerator accessorsForObject
  292. !
  293. initializeProtocolWith: aGenerator
  294. aGenerator initializeProtocolForObject
  295. !
  296. initializeSourceCodesWith: aGenerator
  297. aGenerator initializeForObject
  298. ! !
  299. !Object class methodsFor: 'initialization'!
  300. initialize
  301. "no op"
  302. ! !
  303. Object subclass: #Boolean
  304. slots: {}
  305. package: 'Kernel-Objects'!
  306. !Boolean commentStamp!
  307. I define the protocol for logic testing operations and conditional control structures for the logical values (see the `controlling` protocol).
  308. I have two instances, `true` and `false`.
  309. I am directly mapped to JavaScript Boolean. The `true` and `false` objects are the JavaScript boolean objects.
  310. ## Usage Example:
  311. aBoolean not ifTrue: [ ... ] ifFalse: [ ... ]!
  312. !Boolean methodsFor: 'comparing'!
  313. == aBoolean
  314. <inlineJS: '
  315. if (typeof aBoolean === "boolean") return (self == true) === aBoolean;
  316. else if (aBoolean !!= null && typeof aBoolean === "object") return (self == true) === aBoolean.valueOf();
  317. else return false;
  318. '>
  319. ! !
  320. !Boolean methodsFor: 'controlling'!
  321. & aBoolean
  322. <inlineJS: '
  323. if(self == true) {
  324. return aBoolean;
  325. } else {
  326. return false;
  327. }
  328. '>
  329. !
  330. and: aBlock
  331. ^ self
  332. ifTrue: "aBlock" [ aBlock value ]
  333. ifFalse: [ false ]
  334. !
  335. ifFalse: aBlock
  336. "inlined in the Compiler"
  337. ^ self ifTrue: [] ifFalse: aBlock
  338. !
  339. ifFalse: aBlock ifTrue: anotherBlock
  340. "inlined in the Compiler"
  341. ^ self ifTrue: anotherBlock ifFalse: aBlock
  342. !
  343. ifTrue: aBlock
  344. "inlined in the Compiler"
  345. ^ self ifTrue: aBlock ifFalse: []
  346. !
  347. ifTrue: aBlock ifFalse: anotherBlock
  348. "inlined in the Compiler"
  349. <inlineJS: '
  350. if(self == true) {
  351. return aBlock._value();
  352. } else {
  353. return anotherBlock._value();
  354. }
  355. '>
  356. !
  357. not
  358. ^ self = false
  359. !
  360. or: aBlock
  361. ^ self
  362. ifTrue: [ true ]
  363. ifFalse: "aBlock" [ aBlock value ]
  364. !
  365. | aBoolean
  366. <inlineJS: '
  367. if(self == true) {
  368. return true;
  369. } else {
  370. return aBoolean;
  371. }
  372. '>
  373. ! !
  374. !Boolean methodsFor: 'converting'!
  375. asBit
  376. ^ self ifTrue: [ 1 ] ifFalse: [ 0 ]
  377. !
  378. asJavaScriptObject
  379. ^ self
  380. !
  381. asString
  382. <inlineJS: 'return self.toString()'>
  383. ! !
  384. !Boolean methodsFor: 'copying'!
  385. deepCopy
  386. ^ self
  387. !
  388. shallowCopy
  389. ^ self
  390. ! !
  391. !Boolean methodsFor: 'printing'!
  392. printOn: aStream
  393. aStream nextPutAll: self asString
  394. ! !
  395. !Boolean methodsFor: 'testing'!
  396. isBoolean
  397. ^ true
  398. !
  399. isImmutable
  400. ^ true
  401. ! !
  402. Object subclass: #Date
  403. slots: {}
  404. package: 'Kernel-Objects'!
  405. !Date commentStamp!
  406. I am used to work with both dates and times. Therefore `Date today` and `Date now` are both valid in
  407. Amber and answer the same date object.
  408. Date directly maps to the `Date()` JavaScript constructor, and Amber date objects are JavaScript date objects.
  409. ## API
  410. The class-side `instance creation` protocol contains some convenience methods for creating date/time objects such as `#fromSeconds:`.
  411. Arithmetic and comparison is supported (see the `comparing` and `arithmetic` protocols).
  412. The `converting` protocol provides convenience methods for various convertions (to numbers, strings, etc.).!
  413. !Date methodsFor: 'accessing'!
  414. day
  415. ^ self dayOfWeek
  416. !
  417. day: aNumber
  418. self dayOfWeek: aNumber
  419. !
  420. dayOfMonth
  421. <inlineJS: 'return self.getDate()'>
  422. !
  423. dayOfMonth: aNumber
  424. <inlineJS: 'self.setDate(aNumber)'>
  425. !
  426. dayOfWeek
  427. <inlineJS: 'return self.getDay() + 1'>
  428. !
  429. dayOfWeek: aNumber
  430. <inlineJS: 'return self.setDay(aNumber - 1)'>
  431. !
  432. hours
  433. <inlineJS: 'return self.getHours()'>
  434. !
  435. hours: aNumber
  436. <inlineJS: 'self.setHours(aNumber)'>
  437. !
  438. milliseconds
  439. <inlineJS: 'return self.getMilliseconds()'>
  440. !
  441. milliseconds: aNumber
  442. <inlineJS: 'self.setMilliseconds(aNumber)'>
  443. !
  444. minutes
  445. <inlineJS: 'return self.getMinutes()'>
  446. !
  447. minutes: aNumber
  448. <inlineJS: 'self.setMinutes(aNumber)'>
  449. !
  450. month
  451. <inlineJS: 'return self.getMonth() + 1'>
  452. !
  453. month: aNumber
  454. <inlineJS: 'self.setMonth(aNumber - 1)'>
  455. !
  456. seconds
  457. <inlineJS: 'return self.getSeconds()'>
  458. !
  459. seconds: aNumber
  460. <inlineJS: 'self.setSeconds(aNumber)'>
  461. !
  462. time
  463. <inlineJS: 'return self.getTime()'>
  464. !
  465. time: aNumber
  466. <inlineJS: 'self.setTime(aNumber)'>
  467. !
  468. year
  469. <inlineJS: 'return self.getFullYear()'>
  470. !
  471. year: aNumber
  472. <inlineJS: 'self.setFullYear(aNumber)'>
  473. ! !
  474. !Date methodsFor: 'arithmetic'!
  475. + aNumber
  476. <inlineJS: 'return new Date($self.getTime() + aNumber)'>
  477. !
  478. - aDate
  479. <inlineJS: 'return self - aDate'>
  480. ! !
  481. !Date methodsFor: 'comparing'!
  482. < aDate
  483. <inlineJS: 'return self < aDate'>
  484. !
  485. <= aDate
  486. <inlineJS: 'return self <= aDate'>
  487. !
  488. = aDate
  489. ^ (aDate class == self class) and: [ self asMilliseconds == aDate asMilliseconds ]
  490. !
  491. > aDate
  492. <inlineJS: 'return self > aDate'>
  493. !
  494. >= aDate
  495. <inlineJS: 'return self >= aDate'>
  496. ! !
  497. !Date methodsFor: 'converting'!
  498. asDateString
  499. <inlineJS: 'return self.toDateString()'>
  500. !
  501. asLocaleString
  502. <inlineJS: 'return self.toLocaleString()'>
  503. !
  504. asMilliseconds
  505. ^ self time
  506. !
  507. asNumber
  508. ^ self asMilliseconds
  509. !
  510. asString
  511. <inlineJS: 'return self.toString()'>
  512. !
  513. asTimeString
  514. <inlineJS: 'return self.toTimeString()'>
  515. ! !
  516. !Date methodsFor: 'printing'!
  517. printOn: aStream
  518. aStream nextPutAll: self asString
  519. ! !
  520. !Date class methodsFor: 'accessing'!
  521. classTag
  522. "Returns a tag or general category for this class.
  523. Typically used to help tools do some reflection.
  524. Helios, for example, uses this to decide what icon the class should display."
  525. ^ 'magnitude'
  526. ! !
  527. !Date class methodsFor: 'instance creation'!
  528. fromMilliseconds: aNumber
  529. ^ self new: aNumber
  530. !
  531. fromSeconds: aNumber
  532. ^ self fromMilliseconds: aNumber * 1000
  533. !
  534. fromString: aString
  535. "Example: Date fromString('2011/04/15 00:00:00')"
  536. ^ self new: aString
  537. !
  538. millisecondsToRun: aBlock
  539. | t |
  540. t := Date now.
  541. aBlock value.
  542. ^ Date now - t
  543. !
  544. new: anObject
  545. <inlineJS: 'return new Date(anObject)'>
  546. !
  547. now
  548. ^ self today
  549. !
  550. today
  551. ^ self new
  552. ! !
  553. Object subclass: #Number
  554. slots: {}
  555. package: 'Kernel-Objects'!
  556. !Number commentStamp!
  557. I am the Amber representation for all numbers.
  558. I am directly mapped to JavaScript Number.
  559. ## API
  560. I provide all necessary methods for arithmetic operations, comparison, conversion and so on with numbers.
  561. My instances can also be used to evaluate a block a fixed number of times:
  562. 5 timesRepeat: [ Transcript show: 'This will be printed 5 times'; cr ].
  563. 1 to: 5 do: [ :aNumber| Transcript show: aNumber asString; cr ].
  564. 1 to: 10 by: 2 do: [ :aNumber| Transcript show: aNumber asString; cr ].!
  565. !Number methodsFor: 'arithmetic'!
  566. * aNumber
  567. <inlineJS: 'return self * aNumber'>
  568. !
  569. + aNumber
  570. <inlineJS: 'return self + aNumber'>
  571. !
  572. - aNumber
  573. <inlineJS: 'return self - aNumber'>
  574. !
  575. / aNumber
  576. <inlineJS: 'return self / aNumber'>
  577. !
  578. // aNumber
  579. ^ (self / aNumber) floor
  580. !
  581. \\ aNumber
  582. <inlineJS: 'return self % aNumber'>
  583. !
  584. abs
  585. <inlineJS: 'return Math.abs(self);'>
  586. !
  587. max: aNumber
  588. <inlineJS: 'return Math.max(self, aNumber);'>
  589. !
  590. min: aNumber
  591. <inlineJS: 'return Math.min(self, aNumber);'>
  592. !
  593. min: aMin max: aMax
  594. ^ (self min: aMin) max: aMax
  595. !
  596. negated
  597. ^ 0 - self
  598. ! !
  599. !Number methodsFor: 'comparing'!
  600. < aNumber
  601. <inlineJS: 'return self < aNumber'>
  602. !
  603. <= aNumber
  604. <inlineJS: 'return self <= aNumber'>
  605. !
  606. == aNumber
  607. <inlineJS: '
  608. if (typeof aNumber === "number") return Number(self) === aNumber;
  609. else if (aNumber !!= null && typeof aNumber === "object") return Number(self) === aNumber.valueOf();
  610. else return false;
  611. '>
  612. !
  613. > aNumber
  614. <inlineJS: 'return self > aNumber'>
  615. !
  616. >= aNumber
  617. <inlineJS: 'return self >= aNumber'>
  618. ! !
  619. !Number methodsFor: 'converting'!
  620. & aNumber
  621. <inlineJS: 'return self & aNumber'>
  622. !
  623. @ aNumber
  624. ^ Point x: self y: aNumber
  625. !
  626. asJavaScriptObject
  627. ^ self
  628. !
  629. asJavaScriptSource
  630. ^ '(', self printString, ')'
  631. !
  632. asNumber
  633. ^ self
  634. !
  635. asPoint
  636. ^ Point x: self y: self
  637. !
  638. asString
  639. <inlineJS: 'return String(self)'>
  640. !
  641. atRandom
  642. ^ (Random new next * self) truncated + 1
  643. !
  644. bitAnd: aNumber
  645. <inlineJS: 'return self & aNumber'>
  646. !
  647. bitNot
  648. <inlineJS: 'return ~self'>
  649. !
  650. bitOr: aNumber
  651. <inlineJS: 'return self | aNumber'>
  652. !
  653. bitXor: aNumber
  654. <inlineJS: 'return self ^ aNumber'>
  655. !
  656. ceiling
  657. <inlineJS: 'return Math.ceil(self);'>
  658. !
  659. degreesToRadians
  660. ^ self * Number radiansPerDegree
  661. !
  662. floor
  663. <inlineJS: 'return Math.floor(self);'>
  664. !
  665. printStringBase: aBase
  666. <inlineJS: 'return self.toString(aBase)'>
  667. !
  668. radiansToDegrees
  669. ^ self / Number radiansPerDegree
  670. !
  671. rounded
  672. <inlineJS: 'return Math.round(self);'>
  673. !
  674. to: aNumber
  675. | array first last count |
  676. first := self truncated.
  677. last := aNumber truncated + 1.
  678. count := 1.
  679. array := Array new.
  680. (last - first) timesRepeat: [
  681. array at: count put: first.
  682. count := count + 1.
  683. first := first + 1 ].
  684. ^ array
  685. !
  686. to: stop by: step
  687. | array value pos |
  688. value := self.
  689. array := Array new.
  690. pos := 1.
  691. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  692. step < 0
  693. ifTrue: [ [ value >= stop ] whileTrue: [
  694. array at: pos put: value.
  695. pos := pos + 1.
  696. value := value + step ]]
  697. ifFalse: [ [ value <= stop ] whileTrue: [
  698. array at: pos put: value.
  699. pos := pos + 1.
  700. value := value + step ]].
  701. ^ array
  702. !
  703. truncated
  704. <inlineJS: '
  705. if(self >= 0) {
  706. return Math.floor(self);
  707. } else {
  708. return Math.floor(self * (-1)) * (-1);
  709. };
  710. '>
  711. !
  712. | aNumber
  713. <inlineJS: 'return self | aNumber'>
  714. ! !
  715. !Number methodsFor: 'copying'!
  716. copy
  717. ^ self
  718. !
  719. deepCopy
  720. ^ self copy
  721. ! !
  722. !Number methodsFor: 'enumerating'!
  723. timesRepeat: aBlock
  724. | count |
  725. count := 1.
  726. [ count > self ] whileFalse: [
  727. aBlock value.
  728. count := count + 1 ]
  729. !
  730. to: stop by: step do: aBlock
  731. | value |
  732. value := self.
  733. step = 0 ifTrue: [ self error: 'step must be non-zero' ].
  734. step < 0
  735. ifTrue: [ [ value >= stop ] whileTrue: [
  736. aBlock value: value.
  737. value := value + step ]]
  738. ifFalse: [ [ value <= stop ] whileTrue: [
  739. aBlock value: value.
  740. value := value + step ]]
  741. !
  742. to: stop do: aBlock
  743. "Evaluate aBlock for each number from self to aNumber."
  744. | nextValue |
  745. nextValue := self.
  746. [ nextValue <= stop ]
  747. whileTrue:
  748. [ aBlock value: nextValue.
  749. nextValue := nextValue + 1 ]
  750. ! !
  751. !Number methodsFor: 'mathematical functions'!
  752. ** exponent
  753. ^ self raisedTo: exponent
  754. !
  755. arcCos
  756. <inlineJS: 'return Math.acos(self);'>
  757. !
  758. arcSin
  759. <inlineJS: 'return Math.asin(self);'>
  760. !
  761. arcTan
  762. <inlineJS: 'return Math.atan(self);'>
  763. !
  764. arcTan: aNumber
  765. <inlineJS: 'return Math.atan2(self, aNumber);'>
  766. !
  767. cos
  768. <inlineJS: 'return Math.cos(self);'>
  769. !
  770. ln
  771. <inlineJS: 'return Math.log(self);'>
  772. !
  773. ln1p
  774. <inlineJS: 'return Math.log1p(self);'>
  775. !
  776. log
  777. <inlineJS: 'return Math.log(self) / Math.LN10;'>
  778. !
  779. log: aNumber
  780. <inlineJS: 'return Math.log(self) / Math.log(aNumber);'>
  781. !
  782. raisedTo: exponent
  783. <inlineJS: 'return Math.pow(self, exponent);'>
  784. !
  785. sign
  786. self isZero
  787. ifTrue: [ ^ 0 ].
  788. self positive
  789. ifTrue: [ ^ 1 ]
  790. ifFalse: [ ^ -1 ].
  791. !
  792. sin
  793. <inlineJS: 'return Math.sin(self);'>
  794. !
  795. sqrt
  796. <inlineJS: 'return Math.sqrt(self)'>
  797. !
  798. squared
  799. ^ self * self
  800. !
  801. tan
  802. <inlineJS: 'return Math.tan(self);'>
  803. ! !
  804. !Number methodsFor: 'printing'!
  805. printOn: aStream
  806. aStream nextPutAll: self asString
  807. !
  808. printShowingDecimalPlaces: placesDesired
  809. <inlineJS: 'return self.toFixed(placesDesired)'>
  810. ! !
  811. !Number methodsFor: 'testing'!
  812. between: min and: max
  813. ^ self >= min and: [ self <= max ]
  814. !
  815. even
  816. ^ 0 = (self \\ 2)
  817. !
  818. isImmutable
  819. ^ true
  820. !
  821. isNumber
  822. ^ true
  823. !
  824. isZero
  825. ^ self = 0
  826. !
  827. negative
  828. "Answer whether the receiver is mathematically negative."
  829. ^ self < 0
  830. !
  831. odd
  832. ^ self even not
  833. !
  834. positive
  835. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  836. ^ self >= 0
  837. ! !
  838. !Number class methodsFor: 'accessing'!
  839. classTag
  840. "Returns a tag or general category for this class.
  841. Typically used to help tools do some reflection.
  842. Helios, for example, uses this to decide what icon the class should display."
  843. ^ 'magnitude'
  844. ! !
  845. !Number class methodsFor: 'instance creation'!
  846. e
  847. <inlineJS: 'return Math.E;'>
  848. !
  849. pi
  850. <inlineJS: 'return Math.PI'>
  851. !
  852. radiansPerDegree
  853. ^ (self pi) / 180
  854. ! !
  855. Object subclass: #Point
  856. slots: {#x. #y}
  857. package: 'Kernel-Objects'!
  858. !Point commentStamp!
  859. I represent an x-y pair of numbers usually designating a geometric coordinate.
  860. ## API
  861. Instances are traditionally created using the binary `#@` message to a number:
  862. 100@120
  863. Points can then be arithmetically manipulated:
  864. 100@100 + (10@10)
  865. ...or for example:
  866. (100@100) * 2
  867. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  868. 100@ -100 "but 100@-100 would not parse"!
  869. !Point methodsFor: 'accessing'!
  870. x
  871. ^ x
  872. !
  873. x: aNumber
  874. x := aNumber
  875. !
  876. y
  877. ^ y
  878. !
  879. y: aNumber
  880. y := aNumber
  881. ! !
  882. !Point methodsFor: 'arithmetic'!
  883. * aPoint
  884. ^ Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  885. !
  886. + aPoint
  887. ^ Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  888. !
  889. - aPoint
  890. ^ Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  891. !
  892. / aPoint
  893. ^ Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  894. ! !
  895. !Point methodsFor: 'comparing'!
  896. < aPoint
  897. ^ self x < aPoint x and: [
  898. self y < aPoint y ]
  899. !
  900. <= aPoint
  901. ^ self x <= aPoint x and: [
  902. self y <= aPoint y ]
  903. !
  904. = aPoint
  905. ^ aPoint class = self class and: [
  906. (aPoint x = self x) & (aPoint y = self y) ]
  907. !
  908. > aPoint
  909. ^ self x > aPoint x and: [
  910. self y > aPoint y ]
  911. !
  912. >= aPoint
  913. ^ self x >= aPoint x and: [
  914. self y >= aPoint y ]
  915. ! !
  916. !Point methodsFor: 'converting'!
  917. asPoint
  918. ^ self
  919. ! !
  920. !Point methodsFor: 'geometry'!
  921. angle
  922. ^ self y arcTan: self x
  923. ! !
  924. !Point methodsFor: 'point functions'!
  925. dotProduct: aPoint
  926. ^ (x * aPoint x) + (y * aPoint y)
  927. !
  928. normal
  929. "Answer a Point representing the unit vector rotated 90 deg clockwise. For the zero point return -1@0."
  930. | n d |
  931. n := y negated @ x.
  932. (d := (n x * n x + (n y * n y))) = 0
  933. ifTrue: [ ^ -1 @0 ].
  934. ^ n / d sqrt
  935. !
  936. normalized
  937. | r |
  938. r := self r.
  939. r = 0
  940. ifTrue: [ ^ Point x: 0 y: 0 ]
  941. ifFalse: [ ^ Point x: x / r y: y / r ]
  942. ! !
  943. !Point methodsFor: 'polar coordinates'!
  944. r
  945. ^ ((x * x) + (y * y)) sqrt
  946. ! !
  947. !Point methodsFor: 'printing'!
  948. printOn: aStream
  949. "Print receiver in classic x@y notation."
  950. x printOn: aStream.
  951. aStream nextPutAll: '@'.
  952. (y notNil and: [ y negative ]) ifTrue: [
  953. "Avoid ambiguous @- construct"
  954. aStream space ].
  955. y printOn: aStream
  956. ! !
  957. !Point methodsFor: 'rectangle creation'!
  958. corner: aPoint
  959. ^ Rectangle origin: self corner: aPoint
  960. !
  961. extent: aPoint
  962. ^ Rectangle origin: self extent: aPoint
  963. !
  964. rectangle: aPoint
  965. ^ Rectangle point: self point: aPoint
  966. ! !
  967. !Point methodsFor: 'transforming'!
  968. dist: aPoint
  969. "Answer the distance between aPoint and the receiver."
  970. | dx dy |
  971. dx := aPoint x - x.
  972. dy := aPoint y - y.
  973. ^ (dx * dx + (dy * dy)) sqrt
  974. !
  975. translateBy: delta
  976. "Answer a Point translated by delta (an instance of Point)."
  977. ^ (delta x + x) @ (delta y + y)
  978. ! !
  979. !Point class methodsFor: 'accessing'!
  980. classTag
  981. "Returns a tag or general category for this class.
  982. Typically used to help tools do some reflection.
  983. Helios, for example, uses this to decide what icon the class should display."
  984. ^ 'magnitude'
  985. ! !
  986. !Point class methodsFor: 'instance creation'!
  987. x: aNumber y: anotherNumber
  988. ^ self new
  989. x: aNumber;
  990. y: anotherNumber;
  991. yourself
  992. ! !
  993. Object subclass: #Random
  994. slots: {}
  995. package: 'Kernel-Objects'!
  996. !Random commentStamp!
  997. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  998. ## API
  999. The typical use case it to use the `#next` method like the following:
  1000. Random new next
  1001. 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`
  1002. 10 atRandom
  1003. A random number in a specific interval can be obtained with the following:
  1004. (3 to: 7) atRandom
  1005. Be aware that `#to:` does not create an Interval as in other Smalltalk implementations but in fact an `Array` of numbers, so it's better to use:
  1006. 5 atRandom + 2
  1007. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  1008. #('a' 'b' 'c') atRandom
  1009. As well as letter from a `String`:
  1010. 'abc' atRandom
  1011. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  1012. !Random methodsFor: 'accessing'!
  1013. next
  1014. <inlineJS: 'return Math.random()'>
  1015. !
  1016. next: anInteger
  1017. ^ (1 to: anInteger) collect: [ :each | self next ]
  1018. ! !
  1019. Object subclass: #Rectangle
  1020. slots: {#origin. #corner}
  1021. package: 'Kernel-Objects'!
  1022. !Rectangle commentStamp!
  1023. I represent a Rectangle defined by my two corners.
  1024. The simplest way to create an instance is using Point methods:
  1025. 1@1 corner: 2@2
  1026. WIll create a rectangle with 1@1 as the top left and 2@2 at the bottom right.
  1027. 1@1 extent: 1@1
  1028. Will create the same rectangle, defining an origin and a size instead of an origin and a corner.!
  1029. !Rectangle methodsFor: 'accessing'!
  1030. corner
  1031. ^ corner
  1032. !
  1033. origin
  1034. ^ origin
  1035. ! !
  1036. !Rectangle methodsFor: 'private'!
  1037. setPoint: pt1 point: pt2
  1038. origin := (pt1 x min: pt2 x)@(pt1 y min: pt2 y).
  1039. corner := (pt1 x max: pt2 x)@(pt1 y max: pt2 y).
  1040. ! !
  1041. !Rectangle methodsFor: 'testing'!
  1042. = aRectangle
  1043. ^ origin = aRectangle origin and: [ corner = aRectangle corner ]
  1044. !
  1045. containsPoint: aPoint
  1046. ^ origin <= aPoint and: [ corner >= aPoint ]
  1047. !
  1048. containsRect: aRect
  1049. ^ aRect origin >= origin and: [ aRect corner <= corner ]
  1050. !
  1051. printOn: aStream
  1052. origin printOn: aStream.
  1053. aStream nextPutAll: ' corner: '.
  1054. corner printOn: aStream.
  1055. ! !
  1056. !Rectangle class methodsFor: 'instance creation'!
  1057. origin: anOrigin corner: aCorner
  1058. ^ self basicNew setPoint: anOrigin point: aCorner.
  1059. !
  1060. origin: anOrigin extent: anExtent
  1061. ^ self basicNew setPoint: anOrigin point: anOrigin + anExtent.
  1062. !
  1063. point: anOrigin point: aCorner
  1064. ^ self basicNew setPoint: anOrigin point: aCorner.
  1065. ! !
  1066. Object subclass: #UndefinedObject
  1067. slots: {}
  1068. package: 'Kernel-Objects'!
  1069. !UndefinedObject commentStamp!
  1070. I describe the behavior of my sole instance, `nil`. `nil` represents a prior value for variables that have not been initialized, or for results which are meaningless.
  1071. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  1072. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  1073. !UndefinedObject methodsFor: 'accessing'!
  1074. identityHash
  1075. ^ 'NIL'
  1076. ! !
  1077. !UndefinedObject methodsFor: 'converting'!
  1078. asJavaScriptObject
  1079. ^ null
  1080. ! !
  1081. !UndefinedObject methodsFor: 'copying'!
  1082. deepCopy
  1083. ^ self
  1084. !
  1085. shallowCopy
  1086. ^ self
  1087. ! !
  1088. !UndefinedObject methodsFor: 'evaluating'!
  1089. value
  1090. <inlineJS: 'return null'>
  1091. ! !
  1092. !UndefinedObject methodsFor: 'printing'!
  1093. printOn: aStream
  1094. aStream nextPutAll: 'nil'
  1095. ! !
  1096. !UndefinedObject methodsFor: 'testing'!
  1097. == anObject
  1098. ^ anObject isNil
  1099. !
  1100. ifNil: aBlock
  1101. "inlined in the Compiler"
  1102. ^ self ifNil: aBlock ifNotNil: []
  1103. !
  1104. ifNil: aBlock ifNotNil: anotherBlock
  1105. "inlined in the Compiler"
  1106. ^ aBlock value
  1107. !
  1108. ifNotNil: aBlock
  1109. "inlined in the Compiler"
  1110. ^ self
  1111. !
  1112. ifNotNil: aBlock ifNil: anotherBlock
  1113. "inlined in the Compiler"
  1114. ^ anotherBlock value
  1115. !
  1116. isImmutable
  1117. ^ true
  1118. !
  1119. isNil
  1120. ^ true
  1121. !
  1122. notNil
  1123. ^ false
  1124. ! !
  1125. !UndefinedObject class methodsFor: 'instance creation'!
  1126. new
  1127. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1128. ! !
  1129. UndefinedObject setTraitComposition: {TSubclassable} asTraitComposition!
  1130. ! !