Kernel.st 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. nil subclass: #Object instanceVariableNames: '' category: 'Kernel'! !Object methodsFor: '*Canvas'! appendToBrush: aTagBrush
  2. aTagBrush append: self asString
  3. ! ! !Object methodsFor: '*IDE'! inspect
  4. Inspector new
  5. inspect: self;
  6. open ! inspectOn: anInspector
  7. | variables |
  8. variables := Dictionary new.
  9. variables at: '#self' put: self.
  10. self class instanceVariableNames do: [:each |
  11. variables at: each put: (self instVarAt: each)].
  12. anInspector
  13. setLabel: self printString;
  14. setVariables: variables
  15. ! ! !Object methodsFor: 'accessing'! yourself
  16. ^self
  17. ! class
  18. {'return self.klass'}
  19. ! size
  20. self error: 'Object not indexable'
  21. ! instVarAt: aString
  22. {'
  23. var value = self[''@''+aString];
  24. if(typeof(value) == ''undefined'') {
  25. return nil;
  26. } else {
  27. return value;
  28. }
  29. '}
  30. ! instVarAt: aString put: anObject
  31. {'self[''@'' + aString] = anObject'}
  32. ! basicAt: aString
  33. {'
  34. var value = self[aString];
  35. if(typeof(value) == ''undefined'') {
  36. return nil;
  37. } else {
  38. return value;
  39. }
  40. '}
  41. ! basicAt: aString put: anObject
  42. {'return self[aString] = anObject'}
  43. ! basicDelete: aString
  44. {'delete self[aString]'}
  45. ! ! !Object methodsFor: 'comparing'! = anObject
  46. {'return self == anObject'}
  47. ! ~= anObject
  48. ^(self = anObject) == false
  49. ! ! !Object methodsFor: 'converting'! -> anObject
  50. ^Association key: self value: anObject
  51. ! asString
  52. ^self printString
  53. ! asJavascript
  54. ^self asString
  55. ! asJSON
  56. {'return JSON.stringify(self._asJSONObject())'} ! asJSONObject
  57. | object |
  58. object := Object new.
  59. self class instanceVariableNames do: [:each |
  60. object basicAt: each put: (self instVarAt: each) asJSONObject].
  61. ^object ! ! !Object methodsFor: 'copying'! copy
  62. ^self shallowCopy postCopy
  63. ! shallowCopy
  64. {'
  65. var copy = self.klass._new();
  66. for(var i in self) {
  67. if(/^@.+/.test(i)) {
  68. copy[i] = self[i];
  69. }
  70. }
  71. return copy;
  72. '}
  73. ! deepCopy
  74. {'
  75. var copy = self.klass._new();
  76. for(var i in self) {
  77. if(/^@.+/.test(i)) {
  78. copy[i] = self[i]._deepCopy();
  79. }
  80. }
  81. return copy;
  82. '}.
  83. ! postCopy
  84. ! ! !Object methodsFor: 'error handling'! error: aString
  85. Error signal: aString
  86. ! subclassResponsibility
  87. self error: 'This method is a responsibility of a subclass'
  88. ! shouldNotImplement
  89. self error: 'This method should not be implemented in ', self class name
  90. ! try: aBlock catch: anotherBlock
  91. {'try{aBlock()} catch(e) {anotherBlock(e)}'}
  92. ! doesNotUnderstand: aMessage
  93. MessageNotUnderstood new
  94. receiver: self;
  95. message: aMessage;
  96. signal ! ! !Object methodsFor: 'initialization'! initialize
  97. ! ! !Object methodsFor: 'message handling'! perform: aSymbol
  98. ^self perform: aSymbol withArguments: #()
  99. ! perform: aSymbol withArguments: aCollection
  100. ^self basicPerform: aSymbol asSelector withArguments: aCollection
  101. ! basicPerform: aSymbol
  102. ^self basicPerform: aSymbol withArguments: #()
  103. ! basicPerform: aSymbol withArguments: aCollection
  104. {'return self[aSymbol].apply(self, aCollection);'}
  105. ! ! !Object methodsFor: 'printing'! printString
  106. ^'a ', self class name
  107. ! printNl
  108. {'console.log(self)'}
  109. ! ! !Object methodsFor: 'testing'! isKindOf: aClass
  110. ^(self isMemberOf: aClass)
  111. ifTrue: [true]
  112. ifFalse: [self class inheritsFrom: aClass]
  113. ! isMemberOf: aClass
  114. ^self class = aClass
  115. ! ifNil: aBlock
  116. ^self
  117. ! ifNil: aBlock ifNotNil: anotherBlock
  118. ^anotherBlock value
  119. ! ifNotNil: aBlock
  120. ^aBlock value
  121. ! ifNotNil: aBlock ifNil: anotherBlock
  122. ^aBlock value
  123. ! isNil
  124. ^false
  125. ! notNil
  126. ^self isNil not
  127. ! isClass
  128. ^false
  129. ! isMetaclass
  130. ^false
  131. ! isNumber
  132. ^false
  133. ! isString
  134. ^false
  135. ! isParseFailure
  136. ^false
  137. ! ! !Object class methodsFor: 'initialization'! initialize
  138. "no op" ! ! Object subclass: #Smalltalk instanceVariableNames: '' category: 'Kernel'! !Smalltalk methodsFor: 'accessing'! classes
  139. {'return self.classes()'} ! debugMode
  140. {'return self.debugMode'} ! debugMode: aBoolean
  141. {'self.debugMode = aBoolean'} ! readJSON: anObject
  142. {'return self.readJSObject(anObject);'} ! at: aString
  143. {'return self[aString]'} ! ! Smalltalk class instanceVariableNames: 'current'! !Smalltalk class methodsFor: 'accessing'! current
  144. {'return smalltalk'}
  145. ! ! Object subclass: #Behavior instanceVariableNames: '' category: 'Kernel'! !Behavior methodsFor: 'accessing'! name
  146. {'return self.className || nil'}
  147. ! superclass
  148. {'return self.superclass || nil'}
  149. ! subclasses
  150. {'return smalltalk.subclasses(self)'}
  151. ! allSubclasses
  152. | result |
  153. result := self subclasses.
  154. self subclasses do: [:each |
  155. result addAll: each allSubclasses].
  156. ^result
  157. ! withAllSubclasses
  158. ^(Array with: self) addAll: self allSubclasses; yourself
  159. ! prototype
  160. {'return self.fn.prototype'}
  161. ! methodDictionary
  162. {'var dict = smalltalk.Dictionary._new();
  163. var methods = self.fn.prototype.methods;
  164. for(var i in methods) {
  165. if(methods[i].selector) {
  166. dict._at_put_(methods[i].selector, methods[i]);
  167. }
  168. };
  169. return dict'}
  170. ! methodsFor: aString
  171. ^ClassCategoryReader new
  172. class: self category: aString;
  173. yourself
  174. ! addCompiledMethod: aMethod
  175. {'self.fn.prototype[aMethod.selector._asSelector()] = aMethod.fn;
  176. self.fn.prototype.methods[aMethod.selector] = aMethod;
  177. aMethod.methodClass = self'}
  178. ! instanceVariableNames
  179. {'return self.iVarNames'}
  180. ! comment
  181. ^(self basicAt: 'comment') ifNil: ['']
  182. ! comment: aString
  183. self basicAt: 'comment' put: aString
  184. ! commentStamp
  185. ^ClassCommentReader new
  186. class: self;
  187. yourself
  188. ! removeCompiledMethod: aMethod
  189. {'delete self.fn.prototype[aMethod.selector._asSelector()];
  190. delete self.fn.prototype.methods[aMethod.selector]'}
  191. ! protocols
  192. | protocols |
  193. protocols := Array new.
  194. self methodDictionary do: [:each |
  195. (protocols includes: each category) ifFalse: [
  196. protocols add: each category]].
  197. ^protocols sort ! ! !Behavior methodsFor: 'instance creation'! new
  198. ^self basicNew initialize ! basicNew
  199. {'return new self.fn()'}
  200. ! inheritsFrom: aClass
  201. ^aClass allSubclasses includes: self ! ! Behavior subclass: #Class instanceVariableNames: '' category: 'Kernel'! !Class methodsFor: 'accessing'! category
  202. {'return self.category'} ! category: aString
  203. {'self.category = aString'}
  204. ! rename: aString
  205. {'
  206. smalltalk[aString] = self;
  207. delete smalltalk[self.className];
  208. self.className = aString;
  209. '} ! ! !Class methodsFor: 'class creation'! subclass: aString instanceVariableNames: anotherString
  210. ^self subclass: aString instanceVariableNames: anotherString category: nil
  211. ! subclass: aString instanceVariableNames: aString2 category: aString3
  212. ^ClassBuilder new
  213. superclass: self subclass: aString instanceVariableNames: aString2 category: aString3
  214. ! ! !Class methodsFor: 'printing'! printString
  215. ^self name
  216. ! ! !Class methodsFor: 'testing'! isClass
  217. ^true
  218. ! ! Behavior subclass: #Metaclass instanceVariableNames: '' category: 'Kernel'! !Metaclass methodsFor: 'accessing'! instanceClass
  219. {'return self.instanceClass'} ! instanceVariableNames: aCollection
  220. ClassBuilder new
  221. class: self instanceVariableNames: aCollection
  222. ! ! !Metaclass methodsFor: 'printing'! printString
  223. ^self instanceClass name, ' class'
  224. ! ! !Metaclass methodsFor: 'testing'! isMetaclass
  225. ^true
  226. ! ! Object subclass: #CompiledMethod instanceVariableNames: '' category: 'Kernel'! !CompiledMethod methodsFor: 'accessing'! source
  227. ^(self basicAt: 'source') ifNil: ['']
  228. ! source: aString
  229. self basicAt: 'source' put: aString
  230. ! category
  231. ^(self basicAt: 'category') ifNil: ['']
  232. ! category: aString
  233. self basicAt: 'category' put: aString
  234. ! selector
  235. ^self basicAt: 'selector'
  236. ! selector: aString
  237. self basicAt: 'selector' put: aString
  238. ! fn
  239. ^self basicAt: 'fn'
  240. ! fn: aBlock
  241. self basicAt: 'fn' put: aBlock
  242. ! messageSends
  243. ^self basicAt: 'messageSends' ! methodClass
  244. ^self basicAt: 'methodClass' ! referencedClasses
  245. ^self basicAt: 'referencedClasses' ! ! Object subclass: #Number instanceVariableNames: '' category: 'Kernel'! !Number methodsFor: 'arithmetic'! + aNumber
  246. {'return self + aNumber'} ! - aNumber
  247. {'return self - aNumber'} ! * aNumber
  248. {'return self * aNumber'} ! / aNumber
  249. {'return self / aNumber'} ! max: aNumber
  250. {'return Math.max(self, aNumber);'} ! min: aNumber
  251. {'return Math.min(self, aNumber);'} ! ! !Number methodsFor: 'comparing'! = aNumber
  252. {'return Number(self) == aNumber'} ! > aNumber
  253. {'return self > aNumber'} ! < aNumber
  254. {'return self < aNumber'} ! >= aNumber
  255. {'return self >= aNumber'} ! <= aNumber
  256. {'return self <= aNumber'} ! ! !Number methodsFor: 'converting'! rounded
  257. {'return Math.round(self);'} ! truncated
  258. {'return Math.floor(self);'} ! to: aNumber
  259. | array first last count |
  260. first := self truncated.
  261. last := aNumber truncated + 1.
  262. count := 1.
  263. (first <= last) ifFalse: [self error: 'Wrong interval'].
  264. array := Array new.
  265. (last - first) timesRepeat: [
  266. array at: count put: first.
  267. count := count + 1.
  268. first := first + 1].
  269. ^array
  270. ! asString
  271. ^self printString
  272. ! asJavascript
  273. ^'(', self printString, ')'
  274. ! atRandom
  275. ^(Random new next * self) truncated + 1
  276. ! @ aNumber
  277. ^Point x: self y: aNumber ! asPoint
  278. ^Point x: self y: self ! asJSONObject
  279. ^self ! ! !Number methodsFor: 'enumerating'! timesRepeat: aBlock
  280. | integer count |
  281. integer := self truncated.
  282. count := 1.
  283. [count > self] whileFalse: [
  284. aBlock value.
  285. count := count + 1]
  286. ! to: aNumber do: aBlock
  287. ^(self to: aNumber) do: aBlock
  288. ! ! !Number methodsFor: 'printing'! printString
  289. {'return String(self)'} ! ! !Number methodsFor: 'testing'! isNumber
  290. ^true
  291. ! ! !Number methodsFor: 'timeouts/intervals'! clearInterval
  292. {'clearInterval(Number(self))'} ! clearTimeout
  293. {'clearTimeout(Number(self))'} ! ! !Number class methodsFor: 'instance creation'! pi
  294. {'return Math.PI'} ! ! Object subclass: #BlockClosure instanceVariableNames: '' category: 'Kernel'! !BlockClosure methodsFor: '*Canvas'! appendToBrush: aTagBrush
  295. aTagBrush appendBlock: self ! ! !BlockClosure methodsFor: '*JQuery'! appendToJQuery: aJQuery
  296. | canvas |
  297. canvas := HTMLCanvas new.
  298. self value: canvas.
  299. aJQuery append: canvas
  300. ! ! !BlockClosure methodsFor: 'accessing'! compiledSource
  301. {'return self.toString()'} ! ! !BlockClosure methodsFor: 'controlling'! whileTrue: aBlock
  302. {'while(self()) {aBlock()}'} ! whileFalse: aBlock
  303. {'while(!self()) {aBlock()}'} ! ! !BlockClosure methodsFor: 'error handling'! on: anErrorClass do: aBlock
  304. self try: self catch: [:error |
  305. (error isKindOf: anErrorClass)
  306. ifTrue: [aBlock value: error]
  307. ifFalse: [error signal]] ! ! !BlockClosure methodsFor: 'evaluating'! value
  308. {'return self();'} ! value: anArg
  309. {'return self(anArg);'} ! value: firstArg value: secondArg
  310. {'return self(firstArg, secondArg);'} ! value: firstArg value: secondArg value: thirdArg
  311. {'return self(firstArg, secondArg, thirdArg);'} ! valueWithPossibleArguments: aCollection
  312. {'return self.apply(null, aCollection);'} ! ! !BlockClosure methodsFor: 'timeout/interval'! valueWithTimeout: aNumber
  313. {'return setTimeout(self, aNumber)'} ! valueWithInterval: aNumber
  314. {'return setInterval(self, aNumber)'} ! ! Object subclass: #Boolean instanceVariableNames: '' category: 'Kernel'! !Boolean methodsFor: 'comparing'! = aBoolean
  315. {'return Boolean(self == true) == aBoolean'} ! asJSONObject
  316. ^self ! ! !Boolean methodsFor: 'controlling'! ifTrue: aBlock
  317. ^self ifTrue: aBlock ifFalse: []
  318. ! ifFalse: aBlock
  319. ^self ifTrue: [] ifFalse: aBlock
  320. ! ifFalse: aBlock ifTrue: anotherBlock
  321. ^self ifTrue: anotherBlock ifFalse: aBlock
  322. ! ifTrue: aBlock ifFalse: anotherBlock
  323. {'
  324. if(self == true) {
  325. return aBlock();
  326. } else {
  327. return anotherBlock();
  328. }
  329. '} ! and: aBlock
  330. ^self = true
  331. ifTrue: aBlock
  332. ifFalse: [false]
  333. ! or: aBlock
  334. ^self = true
  335. ifTrue: [true]
  336. ifFalse: aBlock
  337. ! not
  338. ^self = false
  339. ! ! !Boolean methodsFor: 'copying'! shallowCopy
  340. ^self
  341. ! deepCopy
  342. ^self
  343. ! ! !Boolean methodsFor: 'printing'! printString
  344. {'return self.toString()'} ! ! Object subclass: #Date instanceVariableNames: '' category: 'Kernel'! !Date commentStamp! The%20Date%20class%20is%20used%20to%20work%20with%20dates%20and%20times.! !Date methodsFor: '*IDE'! inspectOn: anInspector
  345. | variables |
  346. variables := Dictionary new.
  347. variables at: '#self' put: self.
  348. variables at: '#year' put: self year.
  349. variables at: '#month' put: self month.
  350. variables at: '#day' put: self day.
  351. variables at: '#hours' put: self hours.
  352. variables at: '#minutes' put: self minutes.
  353. variables at: '#seconds' put: self seconds.
  354. variables at: '#milliseconds' put: self milliseconds.
  355. anInspector
  356. setLabel: self printString;
  357. setVariables: variables
  358. ! ! !Date methodsFor: 'accessing'! year
  359. {'return self.getFullYear()'} ! month
  360. {'return self.getMonth() + 1'} ! month: aNumber
  361. {'self.setMonth(aNumber - 1)'} ! day
  362. ^self dayOfWeek ! dayOfWeek
  363. {'return self.getDay() + 1'} ! dayOfWeek: aNumber
  364. {'return self.setDay(aNumber - 1)'} ! day: aNumber
  365. self day: aNumber ! year: aNumber
  366. {'self.setFullYear(aNumber)'} ! dayOfMonth
  367. {'return self.getDate()'} ! dayOfMonth: aNumber
  368. {'self.setDate(aNumber)'} ! time
  369. {'return self.getTime()'} ! time: aNumber
  370. {'self.setTime(aNumber)'} ! hours: aNumber
  371. {'self.setHours(aNumber)'} ! minutes: aNumber
  372. {'self.setMinutes(aNumber)'} ! seconds: aNumber
  373. {'self.setSeconds(aNumber)'} ! milliseconds: aNumber
  374. {'self.setMilliseconds(aNumber)'} ! hours
  375. {'return self.getHours()'} ! minutes
  376. {'return self.getMinutes()'} ! seconds
  377. {'return self.getSeconds()'} ! milliseconds
  378. {'return self.getMilliseconds()'} ! ! !Date methodsFor: 'arithmetic'! - aDate
  379. {'return self - aDate'} ! + aDate
  380. {'return self + aDate'} ! ! !Date methodsFor: 'comparing'! < aDate
  381. {'return self < aDate'} ! > aDate
  382. {'return self > aDate'} ! <= aDate
  383. {'self <= aDate'} ! >= aDate
  384. {'self >= aDate'} ! ! !Date methodsFor: 'converting'! asString
  385. {'return self.toString()'} ! asMilliseconds
  386. ^self time ! asDateString
  387. {'return self.toDateString()'} ! asTimeString
  388. {'return self.toTimeString()'} ! asLocaleString
  389. {'return self.toLocaleString()'} ! asNumber
  390. ^self asMilliseconds ! asJSONObject
  391. ^self ! ! !Date methodsFor: 'printing'! printString
  392. ^self asString ! ! !Date class methodsFor: 'instance creation'! new: anObject
  393. {'return new Date(anObject)'} ! fromString: aString
  394. "Example: Date fromString('2011/04/15 00:00:00')"
  395. ^self new: aString ! fromSeconds: aNumber
  396. ^self fromMilliseconds: aNumber * 1000 ! fromMilliseconds: aNumber
  397. ^self new: aNumber ! today
  398. ^self new ! now
  399. ^self today ! millisecondsToRun: aBlock
  400. | t |
  401. t := Date now.
  402. aBlock value.
  403. ^Date now - t ! ! Object subclass: #UndefinedObject instanceVariableNames: '' category: 'Kernel'! !UndefinedObject methodsFor: 'class creation'! subclass: aString instanceVariableNames: anotherString
  404. ^self subclass: aString instanceVariableNames: anotherString category: nil
  405. ! subclass: aString instanceVariableNames: aString2 category: aString3
  406. ^ClassBuilder new
  407. superclass: self subclass: aString instanceVariableNames: aString2 category: aString3
  408. ! ! !UndefinedObject methodsFor: 'copying'! shallowCopy
  409. ^self
  410. ! deepCopy
  411. ^self
  412. ! ! !UndefinedObject methodsFor: 'printing'! printString
  413. ^'nil'
  414. ! ! !UndefinedObject methodsFor: 'testing'! ifNil: aBlock
  415. ^self ifNil: aBlock ifNotNil: []
  416. ! ifNotNil: aBlock
  417. ^self
  418. ! ifNil: aBlock ifNotNil: anotherBlock
  419. ^aBlock value
  420. ! ifNotNil: aBlock ifNil: anotherBlock
  421. ^anotherBlock value
  422. ! isNil
  423. ^true
  424. ! notNil
  425. ^false
  426. ! ! !UndefinedObject class methodsFor: 'instance creation'! new
  427. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  428. ! ! Object subclass: #Collection instanceVariableNames: '' category: 'Kernel'! !Collection methodsFor: '*IDE'! inspectOn: anInspector
  429. | variables |
  430. variables := Dictionary new.
  431. variables at: '#self' put: self.
  432. self withIndexDo: [:each :i |
  433. variables at: i put: each].
  434. anInspector
  435. setLabel: self printString;
  436. setVariables: variables ! ! !Collection methodsFor: 'accessing'! size
  437. self subclassResponsibility
  438. ! readStream
  439. ^self stream
  440. ! writeStream
  441. ^self stream
  442. ! stream
  443. ^self streamClass on: self
  444. ! streamClass
  445. ^self class streamClass
  446. ! ! !Collection methodsFor: 'adding/removing'! add: anObject
  447. self subclassResponsibility
  448. ! addAll: aCollection
  449. aCollection do: [:each |
  450. self add: each].
  451. ^aCollection
  452. ! remove: anObject
  453. self subclassResponsibility
  454. ! ! !Collection methodsFor: 'converting'! asArray
  455. | array index |
  456. array := Array new.
  457. index := 0.
  458. self do: [:each |
  459. index := index + 1.
  460. array at: index put: each].
  461. ^array
  462. ! ! !Collection methodsFor: 'copying'! , aCollection
  463. ^self copy
  464. addAll: aCollection;
  465. yourself
  466. ! copyWith: anObject
  467. ^self copy add: anObject; yourself
  468. ! copyWithAll: aCollection
  469. ^self copy addAll: aCollection; yourself
  470. ! ! !Collection methodsFor: 'enumerating'! do: aBlock
  471. {'for(var i=0;i<self.length;i++){aBlock(self[i]);}'}
  472. ! collect: aBlock
  473. | newCollection |
  474. newCollection := self class new.
  475. self do: [:each |
  476. newCollection add: (aBlock value: each)].
  477. ^newCollection
  478. ! detect: aBlock
  479. ^self detect: aBlock ifNone: [self errorNotFound]
  480. ! detect: aBlock ifNone: anotherBlock
  481. {'
  482. for(var i = 0; i < self.length; i++)
  483. if(aBlock(self[i]))
  484. return self[i];
  485. return anotherBlock();
  486. '} ! do: aBlock separatedBy: anotherBlock
  487. | first |
  488. first := true.
  489. self do: [:each |
  490. first
  491. ifTrue: [first := false]
  492. ifFalse: [anotherBlock value].
  493. aBlock value: each]
  494. ! inject: anObject into: aBlock
  495. | result |
  496. result := anObject.
  497. self do: [:each |
  498. result := aBlock value: result value: each].
  499. ^result
  500. ! reject: aBlock
  501. ^self select: [:each | (aBlock value: each) = false]
  502. ! select: aBlock
  503. | stream |
  504. stream := self class new writeStream.
  505. self do: [:each |
  506. (aBlock value: each) ifTrue: [
  507. stream nextPut: each]].
  508. ^stream contents
  509. ! ! !Collection methodsFor: 'error handling'! errorNotFound
  510. self error: 'Object is not in the collection'
  511. ! ! !Collection methodsFor: 'testing'! includes: anObject
  512. {'
  513. var i = self.length;
  514. while (i--) {
  515. if (smalltalk.send(self[i], "__eq", [anObject])) {return true;}
  516. }
  517. return false
  518. '}
  519. ! notEmpty
  520. ^self isEmpty not
  521. ! isEmpty
  522. ^self size = 0
  523. ! ! !Collection class methodsFor: 'accessing'! streamClass
  524. ^Stream
  525. ! ! !Collection class methodsFor: 'instance creation'! with: anObject
  526. ^self new
  527. add: anObject;
  528. yourself
  529. ! with: anObject with: anotherObject
  530. ^self new
  531. add: anObject;
  532. add: anotherObject;
  533. yourself
  534. ! with: firstObject with: secondObject with: thirdObject
  535. ^self new
  536. add: firstObject;
  537. add: secondObject;
  538. add: thirdObject;
  539. yourself
  540. ! withAll: aCollection
  541. ^self new
  542. addAll: aCollection;
  543. yourself
  544. ! ! Collection subclass: #SequenceableCollection instanceVariableNames: '' category: 'Kernel'! !SequenceableCollection methodsFor: 'accessing'! at: anIndex
  545. ^self at: anIndex ifAbsent: [
  546. self errorNotFound]
  547. ! at: anIndex ifAbsent: aBlock
  548. self subclassResponsibility ! at: anIndex put: anObject
  549. self subclassResponsibility
  550. ! first
  551. ^self at: 1
  552. ! fourth
  553. ^self at: 4
  554. ! last
  555. ^self at: self size
  556. ! second
  557. ^self at: 2
  558. ! third
  559. ^self at: 3
  560. ! allButFirst
  561. ^self copyFrom: 2 to: self size ! allButLast
  562. ^self copyFrom: 1 to: self size - 1 ! indexOf: anObject
  563. ^self indexOf: anObject ifAbsent: [self errorNotFound] ! indexOf: anObject ifAbsent: aBlock
  564. {'for(var i=0;i<self.length;i++){
  565. if(self[i].__eq(anObject)) {return i+1}
  566. }
  567. return aBlock();
  568. '} ! ! !SequenceableCollection methodsFor: 'adding'! removeLast
  569. self remove: self last ! addLast: anObject
  570. self add: anObject ! ! !SequenceableCollection methodsFor: 'copying'! copyFrom: anIndex to: anotherIndex
  571. self subclassResponsibility
  572. ! ! !SequenceableCollection methodsFor: 'enumerating'! withIndexDo: aBlock
  573. {'for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}'} ! ! SequenceableCollection subclass: #String instanceVariableNames: '' category: 'Kernel'! !String methodsFor: '*Canvas'! appendToBrush: aTagBrush
  574. aTagBrush appendString: self
  575. ! ! !String methodsFor: '*IDE'! inspectOn: anInspector
  576. | label |
  577. super inspectOn: anInspector.
  578. self printString size > 30
  579. ifTrue: [label := (self printString copyFrom: 1 to: 30), '...''']
  580. ifFalse: [label := self printString].
  581. anInspector setLabel: label ! ! !String methodsFor: '*JQuery'! asJQuery
  582. ^JQuery fromString: self
  583. ! appendToJQuery: aJQuery
  584. {'aJQuery._appendElement_(String(self))'}
  585. ! ! !String methodsFor: 'accessing'! size
  586. {'return self.length'}
  587. ! at: anIndex
  588. {'return self[anIndex - 1];'} ! at: anIndex put: anObject
  589. self errorReadOnly
  590. ! at: anIndex ifAbsent: aBlock
  591. (self at: anIndex) ifNil: [aBlock]
  592. ! escaped
  593. {'return escape(self)'}
  594. ! unescaped
  595. {'return unescape(self)'} ! ! !String methodsFor: 'adding'! add: anObject
  596. self errorReadOnly
  597. ! remove: anObject
  598. self errorReadOnly
  599. ! ! !String methodsFor: 'comparing'! = aString
  600. {'return String(self) == aString'} ! > aString
  601. {'return String(self) > aString'}
  602. ! < aString
  603. {'return String(self) < aString'}
  604. ! >= aString
  605. {'return String(self) >= aString'}
  606. ! <= aString
  607. {'return String(self) <= aString'} ! ! !String methodsFor: 'converting'! asSelector
  608. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  609. | selector |
  610. selector := '_', self.
  611. selector := selector replace: ':' with: '_'.
  612. selector := selector replace: '[+]' with: '_plus'.
  613. selector := selector replace: '-' with: '_minus'.
  614. selector := selector replace: '[*]' with: '_star'.
  615. selector := selector replace: '[/]' with: '_slash'.
  616. selector := selector replace: '>' with: '_gt'.
  617. selector := selector replace: '<' with: '_lt'.
  618. selector := selector replace: '=' with: '_eq'.
  619. selector := selector replace: ',' with: '_comma'.
  620. selector := selector replace: '[@]' with: '_at'.
  621. ^selector
  622. ! asJavascript
  623. {'
  624. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  625. return "unescape(\"" + escape(self) + "\")";
  626. else
  627. return "\"" + self + "\"";
  628. '} ! tokenize: aString
  629. {'return self.split(aString)'} ! asString
  630. ^self
  631. ! asNumber
  632. {'return Number(self);'} ! asParser
  633. ^PPStringParser new string: self
  634. ! asChoiceParser
  635. ^PPChoiceParser withAll: (self asArray collect: [:each | each asParser])
  636. ! asCharacterParser
  637. ^PPCharacterParser new string: self
  638. ! asJSONObject
  639. ^self ! ! !String methodsFor: 'copying'! , aString
  640. {'return self + aString'} ! copyFrom: anIndex to: anotherIndex
  641. {'return self.substring(anIndex - 1, anotherIndex);'}
  642. ! shallowCopy
  643. ^self class fromString: self
  644. ! deepCopy
  645. ^self shallowCopy
  646. ! ! !String methodsFor: 'error handling'! errorReadOnly
  647. self error: 'Object is read-only'
  648. ! ! !String methodsFor: 'printing'! printString
  649. ^'''', self, ''''
  650. ! printNl
  651. {'console.log(self)'}
  652. ! ! !String methodsFor: 'regular expressions'! replace: aString with: anotherString
  653. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  654. ! replaceRegexp: aRegexp with: aString
  655. {'return self.replace(aRegexp, aString);'}
  656. ! match: aRegexp
  657. {'return self.search(aRegexp) != -1'}
  658. ! ! !String methodsFor: 'testing'! isString
  659. ^true
  660. ! ! !String class methodsFor: 'accessing'! streamClass
  661. ^StringStream
  662. ! cr
  663. {'return ''\n'';'} ! lf
  664. {'return ''\r'';'}
  665. ! space
  666. {'return '' '';'}
  667. ! tab
  668. {'return ''\t'';'} ! crlf
  669. {'return ''\r\n'';'}
  670. ! ! !String class methodsFor: 'instance creation'! fromString: aString
  671. {'return new self.fn(aString);'}
  672. ! ! SequenceableCollection subclass: #Array instanceVariableNames: '' category: 'Kernel'! !Array methodsFor: 'accessing'! size
  673. {'return self.length'} ! at: anIndex put: anObject
  674. {'return self[anIndex - 1] = anObject'}
  675. ! at: anIndex ifAbsent: aBlock
  676. {'
  677. var value = self[anIndex - 1];
  678. if(value === undefined) {
  679. return aBlock();
  680. } else {
  681. return value;
  682. }
  683. '}
  684. ! ! !Array methodsFor: 'adding/removing'! add: anObject
  685. {'self.push(anObject); return anObject;'}
  686. ! remove: anObject
  687. {'
  688. for(var i=0;i<self.length;i++) {
  689. if(self[i] == anObject) {
  690. self.splice(i,1);
  691. break;
  692. }
  693. }
  694. '}
  695. ! removeFrom: aNumber to: anotherNumber
  696. {'self.splice(aNumber - 1,anotherNumber - 1)'} ! ! !Array methodsFor: 'converting'! asJavascript
  697. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  698. ! asJSONObject
  699. ^self collect: [:each | each asJSONObject] ! ! !Array methodsFor: 'copying'! shallowCopy
  700. | newCollection |
  701. newCollection := self class new.
  702. self do: [:each | newCollection add: each].
  703. ^newCollection
  704. ! deepCopy
  705. | newCollection |
  706. newCollection := self class new.
  707. self do: [:each | newCollection add: each deepCopy].
  708. ^newCollection
  709. ! copyFrom: anIndex to: anotherIndex
  710. | array |
  711. array := self class new.
  712. anIndex to: anotherIndex do: [:each |
  713. array add: (self at: each)].
  714. ^array
  715. ! ! !Array methodsFor: 'enumerating'! join: aString
  716. {'return self.join(aString);'} ! sort
  717. ^self basicPerform: 'sort'
  718. ! sort: aBlock
  719. {'
  720. return self.sort(function(a, b) {
  721. if(aBlock(a,b)) {return 1} else {return -1}
  722. })
  723. '} ! sorted
  724. ^self copy sort ! sorted: aBlock
  725. ^self copy sorted: aBlock ! ! Object subclass: #RegularExpression instanceVariableNames: '' category: 'Kernel'! !RegularExpression methodsFor: 'evaluating'! compile: aString
  726. {'return self.compile(aString);'} ! exec: aString
  727. {'return self.exec(aString) || nil'} ! test: aString
  728. {'return self.test(aString);'} ! ! !RegularExpression class methodsFor: 'instance creation'! fromString: aString flag: anotherString
  729. {'return new RegExp(aString, anotherString);'}
  730. ! fromString: aString
  731. ^self fromString: aString flag: ''
  732. ! ! Object subclass: #Error instanceVariableNames: 'messageText' category: 'Kernel'! !Error methodsFor: 'accessing'! messageText
  733. ^messageText
  734. ! messageText: aString
  735. messageText := aString
  736. ! ! !Error methodsFor: 'signaling'! signal
  737. {'console.log(self._messageText()); throw(self)'} ! ! !Error class methodsFor: 'instance creation'! signal: aString
  738. ^self new
  739. messageText: aString;
  740. signal
  741. ! ! Object subclass: #Association instanceVariableNames: 'key, value' category: 'Kernel'! !Association methodsFor: 'accessing'! key: aKey
  742. key := aKey
  743. ! key
  744. ^key
  745. ! value: aValue
  746. value := aValue
  747. ! value
  748. ^value
  749. ! ! !Association methodsFor: 'comparing'! = anAssociation
  750. ^self class = anAssociation class and: [
  751. self key = anAssociation key and: [
  752. self value = anAssociation value]]
  753. ! ! !Association class methodsFor: 'instance creation'! key: aKey value: aValue
  754. ^self new
  755. key: aKey;
  756. value: aValue;
  757. yourself
  758. ! ! Collection subclass: #Dictionary instanceVariableNames: 'keys' category: 'Kernel'! !Dictionary methodsFor: '*IDE'! inspectOn: anInspector
  759. | variables |
  760. variables := Dictionary new.
  761. variables at: '#self' put: self.
  762. variables at: '#keys' put: self keys.
  763. self keysAndValuesDo: [:key :value |
  764. variables at: key put: value].
  765. anInspector
  766. setLabel: self printString;
  767. setVariables: variables ! ! !Dictionary methodsFor: 'accessing'! size
  768. ^keys size
  769. ! associations
  770. | associations |
  771. associations := #().
  772. keys do: [:each |
  773. associations add: (Association key: each value: (self at: each))].
  774. ^associations
  775. ! keys
  776. ^keys copy
  777. ! values
  778. ^keys collect: [:each | self at: each]
  779. ! at: aKey put: aValue
  780. (keys includes: aKey) ifFalse: [keys add: aKey].
  781. ^self basicAt: aKey put: aValue
  782. ! at: aKey ifAbsent: aBlock
  783. ^(self keys includes: aKey)
  784. ifTrue: [self basicAt: aKey]
  785. ifFalse: aBlock ! at: aKey ifAbsentPut: aBlock
  786. ^self at: aKey ifAbsent: [
  787. self at: aKey put: aBlock value]
  788. ! at: aKey ifPresent: aBlock
  789. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  790. ! at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  791. ^(self basicAt: aKey)
  792. ifNil: anotherBlock
  793. ifNotNil: [aBlock value: (self at: aKey)]
  794. ! at: aKey
  795. ^self at: aKey ifAbsent: [self errorNotFound] ! ! !Dictionary methodsFor: 'adding/removing'! add: anAssociation
  796. self at: anAssociation key put: anAssociation value
  797. ! addAll: aDictionary
  798. super addAll: aDictionary associations.
  799. ^aDictionary
  800. ! remove: aKey
  801. self removeKey: aKey
  802. ! removeKey: aKey
  803. keys remove: aKey
  804. ! ! !Dictionary methodsFor: 'comparing'! = aDictionary
  805. self class = aDictionary class ifFalse: [^false].
  806. self associationsDo: [:assoc |
  807. (aDictionary at: assoc key ifAbsent: [^false]) = assoc value
  808. ifFalse: [^false]].
  809. ^true
  810. ! ! !Dictionary methodsFor: 'converting'! asJSONObject
  811. | object |
  812. object := Object new.
  813. self keysAndValuesDo: [:key :value |
  814. object basicAt: key put: value asJSONObject].
  815. ^object ! ! !Dictionary methodsFor: 'copying'! shallowCopy
  816. | copy |
  817. copy := self class new.
  818. self associationsDo: [:each |
  819. copy at: each key put: each value].
  820. ^copy
  821. ! , aCollection
  822. self shouldNotImplement
  823. ! copyFrom: anIndex to: anotherIndex
  824. self shouldNotImplement
  825. ! ! !Dictionary methodsFor: 'enumerating'! associationsDo: aBlock
  826. self associations do: aBlock
  827. ! keysAndValuesDo: aBlock
  828. self associationsDo: [:each |
  829. aBlock value: each key value: each value]
  830. ! do: aBlock
  831. self values do: aBlock
  832. ! select: aBlock
  833. | newDict |
  834. newDict := self class new.
  835. self keysAndValuesDo: [:key :value |
  836. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  837. ^newDict
  838. ! collect: aBlock
  839. | newDict |
  840. newDict := self class new.
  841. self keysAndValuesDo: [:key :value |
  842. newDict at: key put: (aBlock value: value)].
  843. ^newDict
  844. ! detect: aBlock ifNone: anotherBlock
  845. ^self values detect: aBlock ifNone: anotherBlock
  846. ! includes: anObject
  847. ^self values includes: anObject
  848. ! ! !Dictionary methodsFor: 'initialization'! initialize
  849. super initialize.
  850. keys := #()
  851. ! ! Object subclass: #ClassBuilder instanceVariableNames: '' category: 'Kernel'! !ClassBuilder methodsFor: 'class creation'! superclass: aClass subclass: aString
  852. self superclass: aClass subclass: aString instanceVariableNames: '' category: nil
  853. ! superclass: aClass subclass: aString instanceVariableNames: aString2 category: aString3
  854. | newClass |
  855. newClass := self addSubclassOf: aClass named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2).
  856. self setupClass: newClass.
  857. newClass category: (aString3 ifNil: ['unclassified'])
  858. ! class: aClass instanceVariableNames: aString
  859. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  860. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  861. self setupClass: aClass
  862. ! ! !ClassBuilder methodsFor: 'private'! instanceVariableNamesFor: aString
  863. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  864. ! addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  865. {'smalltalk.addClass(aString, aClass, aCollection);
  866. return smalltalk[aString]'} ! setupClass: aClass
  867. {'smalltalk.init(aClass);'}
  868. ! ! Object subclass: #ClassCategoryReader instanceVariableNames: 'class, category, chunkParser' category: 'Kernel'! !ClassCategoryReader methodsFor: 'accessing'! class: aClass category: aString
  869. class := aClass.
  870. category := aString
  871. ! ! !ClassCategoryReader methodsFor: 'fileIn'! scanFrom: aStream
  872. | nextChunk |
  873. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  874. nextChunk isEmptyChunk ifFalse: [
  875. self compileMethod: nextChunk contents.
  876. self scanFrom: aStream].
  877. ! ! !ClassCategoryReader methodsFor: 'initialization'! initialize
  878. super initialize.
  879. chunkParser := ChunkParser new.
  880. ! ! !ClassCategoryReader methodsFor: 'private'! compileMethod: aString
  881. | method |
  882. method := Compiler new load: aString forClass: class.
  883. method category: category.
  884. class addCompiledMethod: method
  885. ! ! Object subclass: #Stream instanceVariableNames: 'collection, position, streamSize' category: 'Kernel'! !Stream methodsFor: 'accessing'! collection
  886. ^collection
  887. ! setCollection: aCollection
  888. collection := aCollection
  889. ! position
  890. ^position ifNil: [position := 0]
  891. ! position: anInteger
  892. position := anInteger
  893. ! streamSize
  894. ^streamSize
  895. ! setStreamSize: anInteger
  896. streamSize := anInteger
  897. ! contents
  898. ^self collection
  899. copyFrom: 1
  900. to: self streamSize
  901. ! size
  902. ^self streamSize
  903. ! ! !Stream methodsFor: 'actions'! reset
  904. self position: 0
  905. ! close
  906. ! flush
  907. ! resetContents
  908. self reset.
  909. self setStreamSize: 0
  910. ! ! !Stream methodsFor: 'enumerating'! do: aBlock
  911. [self atEnd] whileFalse: [aBlock value: self next]
  912. ! ! !Stream methodsFor: 'positioning'! setToEnd
  913. self position: self size
  914. ! skip: anInteger
  915. self position: ((self position + anInteger) min: self size max: 0)
  916. ! ! !Stream methodsFor: 'reading'! next
  917. self position: self position + 1.
  918. ^collection at: self position
  919. ! next: anInteger
  920. | tempCollection |
  921. tempCollection := self collection class new.
  922. anInteger timesRepeat: [
  923. self atEnd ifFalse: [
  924. tempCollection add: self next]].
  925. ^tempCollection
  926. ! peek
  927. ^self atEnd ifFalse: [
  928. self collection at: self position + 1]
  929. ! ! !Stream methodsFor: 'testing'! atEnd
  930. ^self position = self size
  931. ! atStart
  932. ^self position = 0
  933. ! isEmpty
  934. ^self size = 0
  935. ! ! !Stream methodsFor: 'writing'! nextPut: anObject
  936. self position: self position + 1.
  937. self collection at: self position put: anObject.
  938. self setStreamSize: (self streamSize max: self position)
  939. ! nextPutAll: aCollection
  940. aCollection do: [:each |
  941. self nextPut: each]
  942. ! ! !Stream class methodsFor: 'instance creation'! on: aCollection
  943. ^self new
  944. setCollection: aCollection;
  945. setStreamSize: aCollection size;
  946. yourself
  947. ! ! Stream subclass: #StringStream instanceVariableNames: '' category: 'Kernel'! !StringStream methodsFor: 'reading'! next: anInteger
  948. | tempCollection |
  949. tempCollection := self collection class new.
  950. anInteger timesRepeat: [
  951. self atEnd ifFalse: [
  952. tempCollection := tempCollection, self next]].
  953. ^tempCollection
  954. ! ! !StringStream methodsFor: 'writing'! nextPut: aString
  955. self nextPutAll: aString
  956. ! nextPutAll: aString
  957. self setCollection:
  958. (self collection copyFrom: 1 to: self position),
  959. aString,
  960. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  961. self position: self position + aString size.
  962. self setStreamSize: (self streamSize max: self position)
  963. ! cr
  964. ^self nextPutAll: String cr ! crlf
  965. ^self nextPutAll: String crlf ! lf
  966. ^self nextPutAll: String lf ! ! Object subclass: #ClassCommentReader instanceVariableNames: 'class, chunkParser' category: 'Kernel'! !ClassCommentReader methodsFor: 'accessing'! class: aClass
  967. class := aClass
  968. ! ! !ClassCommentReader methodsFor: 'fileIn'! scanFrom: aStream
  969. | nextChunk |
  970. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  971. nextChunk isEmptyChunk ifFalse: [
  972. self setComment: nextChunk contents].
  973. ! ! !ClassCommentReader methodsFor: 'initialization'! initialize
  974. super initialize.
  975. chunkParser := ChunkParser new.
  976. ! ! !ClassCommentReader methodsFor: 'private'! setComment: aString
  977. class comment: aString
  978. ! ! Object subclass: #Random instanceVariableNames: '' category: 'Kernel'! !Random methodsFor: 'accessing'! next
  979. {'return Math.random()'}
  980. ! next: anInteger
  981. ^1 to: anInteger collect: [:each | self next]
  982. ! ! Object subclass: #Point instanceVariableNames: 'x, y' category: 'Kernel'! !Point methodsFor: 'accessing'! x
  983. ^x ! y
  984. ^y ! y: aNumber
  985. y := aNumber ! x: aNumber
  986. x := aNumber ! ! !Point methodsFor: 'arithmetic'! * aPoint
  987. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y ! + aPoint
  988. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y ! - aPoint
  989. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y ! / aPoint
  990. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y ! ! !Point methodsFor: 'converting'! asPoint
  991. ^self ! ! !Point class methodsFor: 'instance creation'! x: aNumber y: anotherNumber
  992. ^self new
  993. x: aNumber;
  994. y: anotherNumber;
  995. yourself ! ! Object subclass: #Message instanceVariableNames: 'selector, arguments' category: 'Kernel'! !Message methodsFor: 'accessing'! selector
  996. ^selector ! selector: aString
  997. selector := aString ! arguments: anArray
  998. arguments := anArray ! arguments
  999. ^arguments ! ! !Message class methodsFor: 'instance creation'! selector: aString arguments: anArray
  1000. ^self new
  1001. selector: aString;
  1002. arguments: anArray;
  1003. yourself ! ! Error subclass: #MessageNotUnderstood instanceVariableNames: 'message, receiver' category: 'Kernel'! !MessageNotUnderstood methodsFor: 'accessing'! message
  1004. ^message ! message: aMessage
  1005. message := aMessage ! receiver
  1006. ^receiver ! receiver: anObject
  1007. receiver := anObject ! messageText
  1008. ^self receiver asString, ' does not understand #', self message selector ! !