Kernel-Objects.st 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. asPoint
  627. ^Point x: self y: self
  628. !
  629. asString
  630. < return String(self) >
  631. !
  632. atRandom
  633. ^(Random new next * self) truncated + 1
  634. !
  635. rounded
  636. <return Math.round(self);>
  637. !
  638. to: aNumber
  639. | array first last count |
  640. first := self truncated.
  641. last := aNumber truncated + 1.
  642. count := 1.
  643. array := Array new.
  644. (last - first) timesRepeat: [
  645. array at: count put: first.
  646. count := count + 1.
  647. first := first + 1].
  648. ^array
  649. !
  650. to: stop by: step
  651. | array value pos |
  652. value := self.
  653. array := Array new.
  654. pos := 1.
  655. step = 0 ifTrue: [self error: 'step must be non-zero'].
  656. step < 0
  657. ifTrue: [[ value >= stop ] whileTrue: [
  658. array at: pos put: value.
  659. pos := pos + 1.
  660. value := value + step]]
  661. ifFalse: [[ value <= stop ] whileTrue: [
  662. array at: pos put: value.
  663. pos := pos + 1.
  664. value := value + step]].
  665. ^array
  666. !
  667. truncated
  668. <
  669. if(self >>= 0) {
  670. return Math.floor(self);
  671. } else {
  672. return Math.floor(self * (-1)) * (-1);
  673. };
  674. >
  675. !
  676. | aNumber
  677. <return self | aNumber>
  678. ! !
  679. !Number methodsFor: 'copying'!
  680. copy
  681. ^self
  682. !
  683. deepCopy
  684. ^self copy
  685. ! !
  686. !Number methodsFor: 'enumerating'!
  687. timesRepeat: aBlock
  688. | count |
  689. count := 1.
  690. [count > self] whileFalse: [
  691. aBlock value.
  692. count := count + 1]
  693. !
  694. to: stop by: step do: aBlock
  695. | value |
  696. value := self.
  697. step = 0 ifTrue: [self error: 'step must be non-zero'].
  698. step < 0
  699. ifTrue: [[ value >= stop ] whileTrue: [
  700. aBlock value: value.
  701. value := value + step]]
  702. ifFalse: [[ value <= stop ] whileTrue: [
  703. aBlock value: value.
  704. value := value + step]]
  705. !
  706. to: stop do: aBlock
  707. "Evaluate aBlock for each number from self to aNumber."
  708. | nextValue |
  709. nextValue := self.
  710. [nextValue <= stop]
  711. whileTrue:
  712. [aBlock value: nextValue.
  713. nextValue := nextValue + 1]
  714. ! !
  715. !Number methodsFor: 'printing'!
  716. printOn: aStream
  717. aStream nextPutAll: self asString
  718. !
  719. printShowingDecimalPlaces: placesDesired
  720. <return self.toFixed(placesDesired)>
  721. ! !
  722. !Number methodsFor: 'testing'!
  723. even
  724. ^ 0 = (self \\ 2)
  725. !
  726. isImmutable
  727. ^ true
  728. !
  729. isNumber
  730. ^true
  731. !
  732. isZero
  733. ^self = 0
  734. !
  735. negative
  736. "Answer whether the receiver is mathematically negative."
  737. ^ self < 0
  738. !
  739. odd
  740. ^ self even not
  741. !
  742. positive
  743. "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
  744. ^ self >= 0
  745. ! !
  746. !Number class methodsFor: 'helios'!
  747. heliosClass
  748. ^ 'magnitude'
  749. ! !
  750. !Number class methodsFor: 'instance creation'!
  751. pi
  752. <return Math.PI>
  753. ! !
  754. Object subclass: #Point
  755. instanceVariableNames: 'x y'
  756. package: 'Kernel-Objects'!
  757. !Point commentStamp!
  758. I represent an x-y pair of numbers usually designating a geometric coordinate.
  759. ## API
  760. Instances are traditionally created using the binary `#@` message to a number:
  761. 100@120
  762. Points can then be arithmetically manipulated:
  763. 100@100 + (10@10)
  764. ...or for example:
  765. (100@100) * 2
  766. **NOTE:** Creating a point with a negative y-value will need a space after `@` in order to avoid a parsing error:
  767. 100@ -100 "but 100@-100 would not parse"!
  768. !Point methodsFor: 'accessing'!
  769. x
  770. ^x
  771. !
  772. x: aNumber
  773. x := aNumber
  774. !
  775. y
  776. ^y
  777. !
  778. y: aNumber
  779. y := aNumber
  780. ! !
  781. !Point methodsFor: 'arithmetic'!
  782. * aPoint
  783. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  784. !
  785. + aPoint
  786. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  787. !
  788. - aPoint
  789. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  790. !
  791. / aPoint
  792. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  793. !
  794. = aPoint
  795. ^aPoint class = self class and: [
  796. (aPoint x = self x) & (aPoint y = self y)]
  797. ! !
  798. !Point methodsFor: 'converting'!
  799. asPoint
  800. ^self
  801. ! !
  802. !Point methodsFor: 'printing'!
  803. printOn: aStream
  804. "Print receiver in classic x@y notation."
  805. x printOn: aStream.
  806. aStream nextPutAll: '@'.
  807. (y notNil and: [y negative]) ifTrue: [
  808. "Avoid ambiguous @- construct"
  809. aStream space ].
  810. y printOn: aStream
  811. ! !
  812. !Point methodsFor: 'transforming'!
  813. translateBy: delta
  814. "Answer a Point translated by delta (an instance of Point)."
  815. ^(delta x + x) @ (delta y + y)
  816. ! !
  817. !Point class methodsFor: 'helios'!
  818. heliosClass
  819. ^ 'magnitude'
  820. ! !
  821. !Point class methodsFor: 'instance creation'!
  822. x: aNumber y: anotherNumber
  823. ^self new
  824. x: aNumber;
  825. y: anotherNumber;
  826. yourself
  827. ! !
  828. Object subclass: #Random
  829. instanceVariableNames: ''
  830. package: 'Kernel-Objects'!
  831. !Random commentStamp!
  832. I an used to generate a random number and I am implemented as a trivial wrapper around javascript `Math.random()`.
  833. ## API
  834. The typical use case it to use the `#next` method like the following:
  835. Random new next
  836. 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`
  837. 10 atRandom
  838. A random number in a specific interval can be obtained with the following:
  839. (3 to: 7) atRandom
  840. 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:
  841. 5 atRandom + 2
  842. Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
  843. #('a' 'b' 'c') atRandom
  844. As well as letter from a `String`:
  845. 'abc' atRandom
  846. Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
  847. !Random methodsFor: 'accessing'!
  848. next
  849. <return Math.random()>
  850. !
  851. next: anInteger
  852. ^(1 to: anInteger) collect: [:each | self next]
  853. ! !
  854. Object subclass: #UndefinedObject
  855. instanceVariableNames: ''
  856. package: 'Kernel-Objects'!
  857. !UndefinedObject commentStamp!
  858. 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.
  859. `nil` is the Smalltalk equivalent of the `undefined` JavaScript object.
  860. __note:__ When sending messages to the `undefined` JavaScript object, it will be replaced by `nil`.!
  861. !UndefinedObject methodsFor: 'class creation'!
  862. subclass: aString instanceVariableNames: anotherString
  863. ^self subclass: aString instanceVariableNames: anotherString package: nil
  864. !
  865. subclass: aString instanceVariableNames: aString2 category: aString3
  866. "Kept for compatibility."
  867. self deprecatedAPI.
  868. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  869. !
  870. subclass: aString instanceVariableNames: aString2 package: aString3
  871. ^ClassBuilder new
  872. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  873. ! !
  874. !UndefinedObject methodsFor: 'converting'!
  875. asJSON
  876. ^null
  877. ! !
  878. !UndefinedObject methodsFor: 'copying'!
  879. deepCopy
  880. ^self
  881. !
  882. shallowCopy
  883. ^self
  884. ! !
  885. !UndefinedObject methodsFor: 'printing'!
  886. printOn: aStream
  887. aStream nextPutAll: 'nil'
  888. ! !
  889. !UndefinedObject methodsFor: 'testing'!
  890. ifNil: aBlock
  891. "inlined in the Compiler"
  892. ^self ifNil: aBlock ifNotNil: []
  893. !
  894. ifNil: aBlock ifNotNil: anotherBlock
  895. "inlined in the Compiler"
  896. ^aBlock value
  897. !
  898. ifNotNil: aBlock
  899. "inlined in the Compiler"
  900. ^self
  901. !
  902. ifNotNil: aBlock ifNil: anotherBlock
  903. "inlined in the Compiler"
  904. ^anotherBlock value
  905. !
  906. isImmutable
  907. ^ true
  908. !
  909. isNil
  910. ^true
  911. !
  912. notNil
  913. ^false
  914. ! !
  915. !UndefinedObject class methodsFor: 'instance creation'!
  916. new
  917. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  918. ! !