1
0

Kernel-Objects.st 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. Smalltalk current createPackage: 'Kernel-Objects'!
  2. nil subclass: #Object
  3. instanceVariableNames: ''
  4. package: 'Kernel-Objects'!
  5. !Object commentStamp!
  6. **I am the root of the Smalltalk class system**. All classes in the system are subclasses of me.
  7. I provide default behavior common to all normal objects, such as:
  8. - accessing
  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. I have no instance variable.
  16. ##Access
  17. Instance variables can be accessed with `#instVarAt:` and `#instVarAt:put:`. `#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. I 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. basicAt: aString
  31. <return self[aString]>
  32. !
  33. basicAt: aString put: anObject
  34. <return self[aString] = anObject>
  35. !
  36. basicDelete: aString
  37. <delete self[aString]; return aString>
  38. !
  39. class
  40. <return self.klass>
  41. !
  42. identityHash
  43. <
  44. var hash=self.identityHash;
  45. if (hash) return hash;
  46. hash=smalltalk.nextId();
  47. Object.defineProperty(self, 'identityHash', {value:hash});
  48. return hash;
  49. >
  50. !
  51. instVarAt: aString
  52. < return self['@'+aString] >
  53. !
  54. instVarAt: aString put: anObject
  55. < self['@' + aString] = anObject >
  56. !
  57. size
  58. self error: 'Object not indexable'
  59. !
  60. value
  61. <return self.valueOf()>
  62. !
  63. yourself
  64. ^self
  65. ! !
  66. !Object methodsFor: 'comparing'!
  67. = anObject
  68. ^self == anObject
  69. !
  70. == anObject
  71. ^self identityHash = anObject identityHash
  72. !
  73. ~= anObject
  74. ^(self = anObject) = false
  75. !
  76. ~~ anObject
  77. ^(self == anObject) = false
  78. ! !
  79. !Object methodsFor: 'converting'!
  80. -> anObject
  81. ^Association key: self value: anObject
  82. !
  83. asJSON
  84. | variables |
  85. variables := HashedCollection new.
  86. self class allInstanceVariableNames do: [:each |
  87. variables at: each put: (self instVarAt: each) asJSON].
  88. ^variables
  89. !
  90. asJSONString
  91. ^JSON stringify: self asJSON
  92. !
  93. asJavascript
  94. ^self asString
  95. !
  96. asString
  97. ^self printString
  98. !
  99. test
  100. | a |
  101. a := 1.
  102. self halt
  103. ! !
  104. !Object methodsFor: 'copying'!
  105. copy
  106. ^self shallowCopy postCopy
  107. !
  108. deepCopy
  109. <
  110. var copy = self.klass._new();
  111. for(var i in self) {
  112. if(/^@.+/.test(i)) {
  113. copy[i] = self[i]._deepCopy();
  114. }
  115. }
  116. return copy;
  117. >
  118. !
  119. postCopy
  120. !
  121. shallowCopy
  122. <
  123. var copy = self.klass._new();
  124. for(var i in self) {
  125. if(/^@.+/.test(i)) {
  126. copy[i] = self[i];
  127. }
  128. }
  129. return copy;
  130. >
  131. ! !
  132. !Object methodsFor: 'error handling'!
  133. deprecatedAPI
  134. "Just a simple way to deprecate methods.
  135. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  136. but it could in the future."
  137. console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'
  138. !
  139. doesNotUnderstand: aMessage
  140. MessageNotUnderstood new
  141. receiver: self;
  142. message: aMessage;
  143. signal
  144. !
  145. error: aString
  146. Error signal: aString
  147. !
  148. halt
  149. self error: 'Halt encountered'
  150. !
  151. shouldNotImplement
  152. self error: 'This method should not be implemented in ', self class name
  153. !
  154. subclassResponsibility
  155. self error: 'This method is a responsibility of a subclass'
  156. !
  157. throw: anObject
  158. < throw anObject >
  159. !
  160. try: aBlock catch: anotherBlock
  161. <try{return aBlock._value()} catch(e) {return anotherBlock._value_(e)}>
  162. ! !
  163. !Object methodsFor: 'initialization'!
  164. initialize
  165. ! !
  166. !Object methodsFor: 'inspecting'!
  167. inspect
  168. InspectorHandler inspect: self
  169. !
  170. inspectOn: anInspector
  171. | variables |
  172. variables := Dictionary new.
  173. variables at: '#self' put: self.
  174. self class allInstanceVariableNames do: [:each |
  175. variables at: each put: (self instVarAt: each)].
  176. anInspector
  177. setLabel: self printString;
  178. setVariables: variables
  179. ! !
  180. !Object methodsFor: 'message handling'!
  181. basicPerform: aString
  182. ^self basicPerform: aString withArguments: #()
  183. !
  184. basicPerform: aString withArguments: aCollection
  185. <return self[aString].apply(self, aCollection);>
  186. !
  187. perform: aString
  188. ^self perform: aString withArguments: #()
  189. !
  190. perform: aString withArguments: aCollection
  191. <return smalltalk.send(self, aString._asSelector(), aCollection)>
  192. ! !
  193. !Object methodsFor: 'printing'!
  194. printOn: aStream
  195. "Append to the aStream, a string representing the receiver."
  196. aStream nextPutAll: (self class name first isVowel
  197. ifTrue: [ 'an ' ]
  198. ifFalse: [ 'a ' ]).
  199. aStream nextPutAll: self class name
  200. !
  201. printString
  202. "Answer a String representation of the receiver."
  203. ^ String streamContents: [ :stream | self printOn: stream ]
  204. ! !
  205. !Object methodsFor: 'streaming'!
  206. putOn: aStream
  207. aStream nextPut: self
  208. ! !
  209. !Object methodsFor: 'testing'!
  210. ifNil: aBlock
  211. "inlined in the Compiler"
  212. ^self
  213. !
  214. ifNil: aBlock ifNotNil: anotherBlock
  215. "inlined in the Compiler"
  216. ^anotherBlock value: self
  217. !
  218. ifNotNil: aBlock
  219. "inlined in the Compiler"
  220. ^aBlock value: self
  221. !
  222. ifNotNil: aBlock ifNil: anotherBlock
  223. "inlined in the Compiler"
  224. ^aBlock value: self
  225. !
  226. isBehavior
  227. ^ false
  228. !
  229. isBoolean
  230. ^ false
  231. !
  232. isClass
  233. ^false
  234. !
  235. isCompiledMethod
  236. ^ false
  237. !
  238. isImmutable
  239. ^ false
  240. !
  241. isKindOf: aClass
  242. ^(self isMemberOf: aClass)
  243. ifTrue: [true]
  244. ifFalse: [self class inheritsFrom: aClass]
  245. !
  246. isMemberOf: aClass
  247. ^self class = aClass
  248. !
  249. isMetaclass
  250. ^false
  251. !
  252. isNil
  253. ^false
  254. !
  255. isNumber
  256. ^false
  257. !
  258. isPackage
  259. ^ false
  260. !
  261. isParseFailure
  262. ^false
  263. !
  264. isString
  265. ^false
  266. !
  267. isSymbol
  268. ^false
  269. !
  270. notNil
  271. ^self isNil not
  272. !
  273. respondsTo: aSelector
  274. ^self class canUnderstand: aSelector
  275. ! !
  276. !Object class methodsFor: 'helios'!
  277. heliosClass
  278. "Should be an Helios extension. Unfortunately, since helios can browse remote
  279. environments, we can't extend base classes"
  280. ^ 'class'
  281. ! !
  282. !Object class methodsFor: 'initialization'!
  283. initialize
  284. "no op"
  285. ! !
  286. Object subclass: #Boolean
  287. instanceVariableNames: ''
  288. package: 'Kernel-Objects'!
  289. !Boolean commentStamp!
  290. I define the protocol for logic testing operations and conditional control structures for the logical values (see the `controlling` protocol).
  291. I have two instances, `true` and `false`.
  292. I am directly mapped to JavaScript Boolean. The `true` and `false` objects are the JavaScript boolean objects.
  293. ## Usage Example:
  294. aBoolean not ifTrue: [ ... ] ifFalse: [ ... ]!
  295. !Boolean methodsFor: 'comparing'!
  296. = aBoolean
  297. <
  298. if(!! aBoolean._isBoolean || !! aBoolean._isBoolean()) {
  299. return false;
  300. }
  301. return Boolean(self == true) == aBoolean
  302. >
  303. !
  304. == aBoolean
  305. ^self = aBoolean
  306. ! !
  307. !Boolean methodsFor: 'controlling'!
  308. & aBoolean
  309. <
  310. if(self == true) {
  311. return aBoolean;
  312. } else {
  313. return false;
  314. }
  315. >
  316. !
  317. and: aBlock
  318. ^self = true
  319. ifTrue: aBlock
  320. ifFalse: [false]
  321. !
  322. ifFalse: aBlock
  323. "inlined in the Compiler"
  324. ^self ifTrue: [] ifFalse: aBlock
  325. !
  326. ifFalse: aBlock ifTrue: anotherBlock
  327. "inlined in the Compiler"
  328. ^self ifTrue: anotherBlock ifFalse: aBlock
  329. !
  330. ifTrue: aBlock
  331. "inlined in the Compiler"
  332. ^self ifTrue: aBlock ifFalse: []
  333. !
  334. ifTrue: aBlock ifFalse: anotherBlock
  335. "inlined in the Compiler"
  336. <
  337. if(self == true) {
  338. return aBlock._value();
  339. } else {
  340. return anotherBlock._value();
  341. }
  342. >
  343. !
  344. not
  345. ^self = false
  346. !
  347. or: aBlock
  348. ^self = true
  349. ifTrue: [true]
  350. ifFalse: aBlock
  351. !
  352. | aBoolean
  353. <
  354. if(self == true) {
  355. return true;
  356. } else {
  357. return aBoolean;
  358. }
  359. >
  360. ! !
  361. !Boolean methodsFor: 'converting'!
  362. asBit
  363. ^ self ifTrue: [ 1 ] ifFalse: [ 0 ]
  364. !
  365. asJSON
  366. ^self
  367. !
  368. asString
  369. < return self.toString() >
  370. ! !
  371. !Boolean methodsFor: 'copying'!
  372. deepCopy
  373. ^self
  374. !
  375. shallowCopy
  376. ^self
  377. ! !
  378. !Boolean methodsFor: 'printing'!
  379. printOn: aStream
  380. aStream nextPutAll: self asString
  381. ! !
  382. !Boolean methodsFor: 'testing'!
  383. isBoolean
  384. ^ true
  385. !
  386. isImmutable
  387. ^ true
  388. ! !
  389. Object subclass: #Date
  390. instanceVariableNames: ''
  391. package: 'Kernel-Objects'!
  392. !Date commentStamp!
  393. I am used to work with both dates and times. Therefore `Date today` and `Date now` are both valid in
  394. Amber and answer the same date object.
  395. Date directly maps to the `Date()` JavaScript constructor, and Amber date objects are JavaScript date objects.
  396. ## API
  397. The class-side `instance creation` protocol contains some convenience methods for creating date/time objects such as `#fromSeconds:`.
  398. Arithmetic and comparison is supported (see the `comparing` and `arithmetic` protocols).
  399. The `converting` protocol provides convenience methods for various convertions (to numbers, strings, etc.).!
  400. !Date methodsFor: 'accessing'!
  401. day
  402. ^self dayOfWeek
  403. !
  404. day: aNumber
  405. self dayOfWeek: aNumber
  406. !
  407. dayOfMonth
  408. <return self.getDate()>
  409. !
  410. dayOfMonth: aNumber
  411. <self.setDate(aNumber)>
  412. !
  413. dayOfWeek
  414. <return self.getDay() + 1>
  415. !
  416. dayOfWeek: aNumber
  417. <return self.setDay(aNumber - 1)>
  418. !
  419. hours
  420. <return self.getHours()>
  421. !
  422. hours: aNumber
  423. <self.setHours(aNumber)>
  424. !
  425. milliseconds
  426. <return self.getMilliseconds()>
  427. !
  428. milliseconds: aNumber
  429. <self.setMilliseconds(aNumber)>
  430. !
  431. minutes
  432. <return self.getMinutes()>
  433. !
  434. minutes: aNumber
  435. <self.setMinutes(aNumber)>
  436. !
  437. month
  438. <return self.getMonth() + 1>
  439. !
  440. month: aNumber
  441. <self.setMonth(aNumber - 1)>
  442. !
  443. seconds
  444. <return self.getSeconds()>
  445. !
  446. seconds: aNumber
  447. <self.setSeconds(aNumber)>
  448. !
  449. time
  450. <return self.getTime()>
  451. !
  452. time: aNumber
  453. <self.setTime(aNumber)>
  454. !
  455. year
  456. <return self.getFullYear()>
  457. !
  458. year: aNumber
  459. <self.setFullYear(aNumber)>
  460. ! !
  461. !Date methodsFor: 'arithmetic'!
  462. + aDate
  463. <return self + aDate>
  464. !
  465. - aDate
  466. <return self - aDate>
  467. ! !
  468. !Date methodsFor: 'comparing'!
  469. < aDate
  470. <return self < aDate>
  471. !
  472. <= aDate
  473. <return self <= aDate>
  474. !
  475. > aDate
  476. <return self >> aDate>
  477. !
  478. >= aDate
  479. <return self >>= aDate>
  480. ! !
  481. !Date methodsFor: 'converting'!
  482. asDateString
  483. <return self.toDateString()>
  484. !
  485. asLocaleString
  486. <return self.toLocaleString()>
  487. !
  488. asMilliseconds
  489. ^self time
  490. !
  491. asNumber
  492. ^self asMilliseconds
  493. !
  494. asString
  495. <return self.toString()>
  496. !
  497. asTimeString
  498. <return self.toTimeString()>
  499. ! !
  500. !Date methodsFor: 'printing'!
  501. printOn: aStream
  502. aStream nextPutAll: self asString
  503. ! !
  504. !Date class methodsFor: 'helios'!
  505. heliosClass
  506. ^ 'magnitude'
  507. ! !
  508. !Date class methodsFor: 'instance creation'!
  509. fromMilliseconds: aNumber
  510. ^self new: aNumber
  511. !
  512. fromSeconds: aNumber
  513. ^self fromMilliseconds: aNumber * 1000
  514. !
  515. fromString: aString
  516. "Example: Date fromString('2011/04/15 00:00:00')"
  517. ^self new: aString
  518. !
  519. millisecondsToRun: aBlock
  520. | t |
  521. t := Date now.
  522. aBlock value.
  523. ^Date now - t
  524. !
  525. new: anObject
  526. <return new Date(anObject)>
  527. !
  528. now
  529. ^self today
  530. !
  531. today
  532. ^self new
  533. ! !
  534. Object subclass: #Number
  535. instanceVariableNames: ''
  536. package: 'Kernel-Objects'!
  537. !Number commentStamp!
  538. I am the Amber representation for all numbers.
  539. I am directly mapped to JavaScript Number.
  540. ## API
  541. I provide all necessary methods for arithmetic operations, comparison, conversion and so on with numbers.
  542. My instances can also be used to evaluate a block a fixed number of times:
  543. 5 timesRepeat: [Transcript show: 'This will be printed 5 times'; cr].
  544. 1 to: 5 do: [:aNumber| Transcript show: aNumber asString; cr].
  545. 1 to: 10 by: 2 do: [:aNumber| Transcript show: aNumber asString; cr].!
  546. !Number methodsFor: 'accessing'!
  547. identityHash
  548. ^self asString, 'n'
  549. ! !
  550. !Number methodsFor: 'arithmetic'!
  551. * aNumber
  552. "Inlined in the Compiler"
  553. <return self * aNumber>
  554. !
  555. + aNumber
  556. "Inlined in the Compiler"
  557. <return self + aNumber>
  558. !
  559. - aNumber
  560. "Inlined in the Compiler"
  561. <return self - aNumber>
  562. !
  563. / aNumber
  564. "Inlined in the Compiler"
  565. <return self / aNumber>
  566. !
  567. \\ aNumber
  568. <return self % aNumber>
  569. !
  570. abs
  571. <return Math.abs(self);>
  572. !
  573. max: aNumber
  574. <return Math.max(self, aNumber);>
  575. !
  576. min: aNumber
  577. <return Math.min(self, aNumber);>
  578. !
  579. negated
  580. ^0 - self
  581. !
  582. sqrt
  583. <return Math.sqrt(self)>
  584. !
  585. squared
  586. ^self * self
  587. ! !
  588. !Number methodsFor: 'comparing'!
  589. < aNumber
  590. "Inlined in the Compiler"
  591. <return self < aNumber>
  592. !
  593. <= aNumber
  594. "Inlined in the Compiler"
  595. <return self <= aNumber>
  596. !
  597. = aNumber
  598. <
  599. if(!! aNumber._isNumber || !! aNumber._isNumber()) {
  600. return false;
  601. }
  602. return Number(self) == aNumber
  603. >
  604. !
  605. > aNumber
  606. "Inlined in the Compiler"
  607. <return self >> aNumber>
  608. !
  609. >= aNumber
  610. "Inlined in the Compiler"
  611. <return self >>= aNumber>
  612. ! !
  613. !Number methodsFor: 'converting'!
  614. & aNumber
  615. <return self & aNumber>
  616. !
  617. @ aNumber
  618. ^Point x: self y: aNumber
  619. !
  620. asJSON
  621. ^self
  622. !
  623. asJavascript
  624. ^'(', self printString, ')'
  625. !
  626. asNumber
  627. ^ self
  628. !
  629. asPoint
  630. ^Point x: self y: self
  631. !
  632. asString
  633. < return String(self) >
  634. !
  635. atRandom
  636. ^(Random new next * self) truncated + 1
  637. !
  638. ceiling
  639. <return Math.ceil(self);>
  640. !
  641. floor
  642. <return Math.floor(self);>
  643. !
  644. rounded
  645. <return Math.round(self);>
  646. !
  647. to: aNumber
  648. | array first last count |
  649. first := self truncated.
  650. last := aNumber truncated + 1.
  651. count := 1.
  652. array := Array new.
  653. (last - first) timesRepeat: [
  654. array at: count put: first.
  655. count := count + 1.
  656. first := first + 1].
  657. ^array
  658. !
  659. to: stop by: step
  660. | array value pos |
  661. value := self.
  662. array := Array new.
  663. pos := 1.
  664. step = 0 ifTrue: [self error: 'step must be non-zero'].
  665. step < 0
  666. ifTrue: [[ value >= stop ] whileTrue: [
  667. array at: pos put: value.
  668. pos := pos + 1.
  669. value := value + step]]
  670. ifFalse: [[ value <= stop ] whileTrue: [
  671. array at: pos put: value.
  672. pos := pos + 1.
  673. value := value + step]].
  674. ^array
  675. !
  676. truncated
  677. <
  678. if(self >>= 0) {
  679. return Math.floor(self);
  680. } else {
  681. return Math.floor(self * (-1)) * (-1);
  682. };
  683. >
  684. !
  685. | aNumber
  686. <return self | aNumber>
  687. ! !
  688. !Number methodsFor: 'copying'!
  689. copy
  690. ^self
  691. !
  692. deepCopy
  693. ^self copy
  694. ! !
  695. !Number methodsFor: 'enumerating'!
  696. timesRepeat: aBlock
  697. | count |
  698. count := 1.
  699. [count > self] whileFalse: [
  700. aBlock value.
  701. count := count + 1]
  702. !
  703. to: stop by: step do: aBlock
  704. | value |
  705. value := self.
  706. step = 0 ifTrue: [self error: 'step must be non-zero'].
  707. step < 0
  708. ifTrue: [[ value >= stop ] whileTrue: [
  709. aBlock value: value.
  710. value := value + step]]
  711. ifFalse: [[ value <= stop ] whileTrue: [
  712. aBlock value: value.
  713. value := value + step]]
  714. !
  715. to: stop do: aBlock
  716. "Evaluate aBlock for each number from self to aNumber."
  717. | nextValue |
  718. nextValue := self.
  719. [nextValue <= stop]
  720. whileTrue:
  721. [aBlock value: nextValue.
  722. nextValue := nextValue + 1]
  723. ! !
  724. !Number methodsFor: 'printing'!
  725. printOn: aStream
  726. aStream nextPutAll: self asString
  727. !
  728. printShowingDecimalPlaces: placesDesired
  729. <return self.toFixed(placesDesired)>
  730. ! !
  731. !Number methodsFor: 'testing'!
  732. even
  733. ^ 0 = (self \\ 2)
  734. !
  735. isImmutable
  736. ^ true
  737. !
  738. isNumber
  739. ^true
  740. !
  741. isZero
  742. ^self = 0
  743. !
  744. negative
  745. "Answer whether the receiver is mathematically negative."
  746. ^ self < 0
  747. !
  748. odd
  749. ^ self even not
  750. !
  751. positive
  752. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  753. ^ self >= 0
  754. ! !
  755. !Number class methodsFor: 'helios'!
  756. heliosClass
  757. ^ 'magnitude'
  758. ! !
  759. !Number class methodsFor: 'instance creation'!
  760. pi
  761. <return Math.PI>
  762. ! !
  763. Object subclass: #Point
  764. instanceVariableNames: 'x y'
  765. package: 'Kernel-Objects'!
  766. !Point commentStamp!
  767. I represent an x-y pair of numbers usually designating a geometric coordinate.
  768. ## API
  769. Instances are traditionally created using the binary `#@` message to a number:
  770. 100@120
  771. Points can then be arithmetically manipulated:
  772. 100@100 + (10@10)
  773. ...or for example:
  774. (100@100) * 2
  775. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  776. 100@ -100 "but 100@-100 would not parse"!
  777. !Point methodsFor: 'accessing'!
  778. x
  779. ^x
  780. !
  781. x: aNumber
  782. x := aNumber
  783. !
  784. y
  785. ^y
  786. !
  787. y: aNumber
  788. y := aNumber
  789. ! !
  790. !Point methodsFor: 'arithmetic'!
  791. * aPoint
  792. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  793. !
  794. + aPoint
  795. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  796. !
  797. - aPoint
  798. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  799. !
  800. / aPoint
  801. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  802. !
  803. = aPoint
  804. ^aPoint class = self class and: [
  805. (aPoint x = self x) & (aPoint y = self y)]
  806. ! !
  807. !Point methodsFor: 'converting'!
  808. asPoint
  809. ^self
  810. ! !
  811. !Point methodsFor: 'printing'!
  812. printOn: aStream
  813. "Print receiver in classic x@y notation."
  814. x printOn: aStream.
  815. aStream nextPutAll: '@'.
  816. (y notNil and: [y negative]) ifTrue: [
  817. "Avoid ambiguous @- construct"
  818. aStream space ].
  819. y printOn: aStream
  820. ! !
  821. !Point methodsFor: 'transforming'!
  822. translateBy: delta
  823. "Answer a Point translated by delta (an instance of Point)."
  824. ^(delta x + x) @ (delta y + y)
  825. ! !
  826. !Point class methodsFor: 'helios'!
  827. heliosClass
  828. ^ 'magnitude'
  829. ! !
  830. !Point class methodsFor: 'instance creation'!
  831. x: aNumber y: anotherNumber
  832. ^self new
  833. x: aNumber;
  834. y: anotherNumber;
  835. yourself
  836. ! !
  837. Object subclass: #Random
  838. instanceVariableNames: ''
  839. package: 'Kernel-Objects'!
  840. !Random commentStamp!
  841. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  842. ## API
  843. The typical use case it to use the `#next` method like the following:
  844. Random new next
  845. 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`
  846. 10 atRandom
  847. A random number in a specific interval can be obtained with the following:
  848. (3 to: 7) atRandom
  849. 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:
  850. 5 atRandom + 2
  851. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  852. #('a' 'b' 'c') atRandom
  853. As well as letter from a `String`:
  854. 'abc' atRandom
  855. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  856. !Random methodsFor: 'accessing'!
  857. next
  858. <return Math.random()>
  859. !
  860. next: anInteger
  861. ^(1 to: anInteger) collect: [:each | self next]
  862. ! !
  863. Object subclass: #UndefinedObject
  864. instanceVariableNames: ''
  865. package: 'Kernel-Objects'!
  866. !UndefinedObject commentStamp!
  867. 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.
  868. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  869. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  870. !UndefinedObject methodsFor: 'class creation'!
  871. subclass: aString instanceVariableNames: anotherString
  872. ^self subclass: aString instanceVariableNames: anotherString package: nil
  873. !
  874. subclass: aString instanceVariableNames: aString2 category: aString3
  875. "Kept for compatibility."
  876. self deprecatedAPI.
  877. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  878. !
  879. subclass: aString instanceVariableNames: aString2 package: aString3
  880. ^ClassBuilder new
  881. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  882. ! !
  883. !UndefinedObject methodsFor: 'converting'!
  884. asJSON
  885. ^null
  886. ! !
  887. !UndefinedObject methodsFor: 'copying'!
  888. deepCopy
  889. ^self
  890. !
  891. shallowCopy
  892. ^self
  893. ! !
  894. !UndefinedObject methodsFor: 'printing'!
  895. printOn: aStream
  896. aStream nextPutAll: 'nil'
  897. ! !
  898. !UndefinedObject methodsFor: 'testing'!
  899. ifNil: aBlock
  900. "inlined in the Compiler"
  901. ^self ifNil: aBlock ifNotNil: []
  902. !
  903. ifNil: aBlock ifNotNil: anotherBlock
  904. "inlined in the Compiler"
  905. ^aBlock value
  906. !
  907. ifNotNil: aBlock
  908. "inlined in the Compiler"
  909. ^self
  910. !
  911. ifNotNil: aBlock ifNil: anotherBlock
  912. "inlined in the Compiler"
  913. ^anotherBlock value
  914. !
  915. isImmutable
  916. ^ true
  917. !
  918. isNil
  919. ^true
  920. !
  921. notNil
  922. ^false
  923. ! !
  924. !UndefinedObject class methodsFor: 'instance creation'!
  925. new
  926. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  927. ! !