2
0

Nemo.st 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. Smalltalk current createPackage: 'Nemo' properties: #{}!
  2. Object subclass: #NemoConnection
  3. instanceVariableNames: 'socket'
  4. package: 'Nemo'!
  5. !NemoConnection methodsFor: 'not yet classified'!
  6. close
  7. socket close.
  8. socket := nil
  9. !
  10. createDefaultSocket
  11. self createSocketOn: self defaultURL.
  12. !
  13. createLocalSocketOn: aPort
  14. ^ self createSocketOn: 'ws://localhost:', aPort asString, '/nemo'
  15. !
  16. createSocketOn: uri
  17. socket := <new WebSocket(uri)>.
  18. socket at: 'onmessage' put: [ :message | self handleMessage: message ].
  19. socket at: 'onerror' put: [ :err | console log: err ]
  20. !
  21. defaultURL
  22. ^ self class defaultURL
  23. !
  24. handleMessage: aMessage
  25. | string result |
  26. string := aMessage data.
  27. string isString ifFalse: [ ^ self ].
  28. [ result := Compiler new evaluateExpression: string ]
  29. on: Error
  30. do: [ :ex | ^ socket send: ex asNemoString ].
  31. socket send: result asNemoString
  32. !
  33. send: aString
  34. socket send: aString
  35. ! !
  36. NemoConnection class instanceVariableNames: 'default'!
  37. !NemoConnection class methodsFor: 'not yet classified'!
  38. default
  39. ^ default ifNil: [ default := self new ]
  40. !
  41. defaultURL
  42. ^ 'ws://localhost:8010/nemo'
  43. ! !
  44. !Object methodsFor: '*Nemo'!
  45. asNemo
  46. ^ self asJSON
  47. !
  48. asNemoString
  49. ^JSON stringify: self asNemo
  50. ! !
  51. !Class methodsFor: '*Nemo'!
  52. asNemo
  53. ^ Dictionary new
  54. at: 'name' put: self name;
  55. at: 'superclass' put: (self superclass ifNotNil: [ self superclass name ]);
  56. at: 'classComment' put: self comment;
  57. at: 'definition' put: self definition;
  58. at: 'package' put: self category;
  59. at: 'instVarNames' put: self instanceVariableNames;
  60. asNemo
  61. ! !
  62. !Metaclass methodsFor: '*Nemo'!
  63. asNemo
  64. ^ Dictionary new
  65. at: 'definition' put: self definition;
  66. at: 'instVarNames' put: self instanceVariableNames;
  67. asNemo
  68. ! !
  69. !Collection methodsFor: '*Nemo'!
  70. asNemo
  71. ^self asArray collect: [:each | each asNemo]
  72. ! !
  73. !HashedCollection methodsFor: '*Nemo'!
  74. asNemo
  75. | c |
  76. c := self class new.
  77. self keysAndValuesDo: [:key :value |
  78. c at: key put: value asNemo].
  79. ^c
  80. ! !
  81. !Dictionary methodsFor: '*Nemo'!
  82. asNemo
  83. ^self asHashedCollection asNemo
  84. ! !
  85. !String methodsFor: '*Nemo'!
  86. asNemo
  87. ^ self
  88. ! !
  89. !CompiledMethod methodsFor: '*Nemo'!
  90. asNemo
  91. ^ Dictionary new
  92. at: 'name' put: self selector;
  93. at: 'protocol' put: self category;
  94. at: 'sourceCode' put: self source;
  95. at: 'compiledSource' put: self fn compiledSource;
  96. at: 'messageSends' put: self messageSends;
  97. asNemo
  98. ! !
  99. !Error methodsFor: '*Nemo'!
  100. asNemo
  101. ^ Dictionary new
  102. at: 'error' put: self messageText;
  103. asNemo
  104. ! !
  105. !Package methodsFor: '*Nemo'!
  106. asNemo
  107. ^ Dictionary new
  108. at: 'name' put: self name;
  109. asNemo
  110. ! !