prism-cli 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ /*
2
+ ** mruby/version.h - mruby version definition
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ */
6
+
7
+ #ifndef MRUBY_VERSION_H
8
+ #define MRUBY_VERSION_H
9
+
10
+ #include "common.h"
11
+
12
+ /**
13
+ * mruby version definition macros
14
+ */
15
+ MRB_BEGIN_DECL
16
+
17
+ /*
18
+ * A passed in expression.
19
+ */
20
+ #define MRB_STRINGIZE0(expr) #expr
21
+
22
+ /*
23
+ * Passes in an expression to MRB_STRINGIZE0.
24
+ */
25
+ #define MRB_STRINGIZE(expr) MRB_STRINGIZE0(expr)
26
+
27
+ /*
28
+ * The version of Ruby used by mruby.
29
+ */
30
+ #define MRUBY_RUBY_VERSION "2.0"
31
+
32
+ /*
33
+ * Ruby engine.
34
+ */
35
+ #define MRUBY_RUBY_ENGINE "mruby"
36
+
37
+ /*
38
+ * Major release version number.
39
+ */
40
+ #define MRUBY_RELEASE_MAJOR 2
41
+
42
+ /*
43
+ * Minor release version number.
44
+ */
45
+ #define MRUBY_RELEASE_MINOR 0
46
+
47
+ /*
48
+ * Tiny release version number.
49
+ */
50
+ #define MRUBY_RELEASE_TEENY 1
51
+
52
+ /*
53
+ * The mruby version.
54
+ */
55
+ #define MRUBY_VERSION MRB_STRINGIZE(MRUBY_RELEASE_MAJOR) "." MRB_STRINGIZE(MRUBY_RELEASE_MINOR) "." MRB_STRINGIZE(MRUBY_RELEASE_TEENY)
56
+
57
+ /*
58
+ * Release number.
59
+ */
60
+ #define MRUBY_RELEASE_NO (MRUBY_RELEASE_MAJOR * 100 * 100 + MRUBY_RELEASE_MINOR * 100 + MRUBY_RELEASE_TEENY)
61
+
62
+ /*
63
+ * Release year.
64
+ */
65
+ #define MRUBY_RELEASE_YEAR 2019
66
+
67
+ /*
68
+ * Release month.
69
+ */
70
+ #define MRUBY_RELEASE_MONTH 4
71
+
72
+ /*
73
+ * Release day.
74
+ */
75
+ #define MRUBY_RELEASE_DAY 4
76
+
77
+ /*
78
+ * Release date as a string.
79
+ */
80
+ #define MRUBY_RELEASE_DATE MRB_STRINGIZE(MRUBY_RELEASE_YEAR) "-" MRB_STRINGIZE(MRUBY_RELEASE_MONTH) "-" MRB_STRINGIZE(MRUBY_RELEASE_DAY)
81
+
82
+ /*
83
+ * The year mruby was first created.
84
+ */
85
+ #define MRUBY_BIRTH_YEAR 2010
86
+
87
+ /*
88
+ * MRuby's authors.
89
+ */
90
+ #define MRUBY_AUTHOR "mruby developers"
91
+
92
+ /*
93
+ * mruby's version, and release date.
94
+ */
95
+ #define MRUBY_DESCRIPTION \
96
+ "mruby " MRUBY_VERSION \
97
+ " (" MRUBY_RELEASE_DATE ") " \
98
+
99
+ /*
100
+ * mruby's copyright information.
101
+ */
102
+ #define MRUBY_COPYRIGHT \
103
+ "mruby - Copyright (c) " \
104
+ MRB_STRINGIZE(MRUBY_BIRTH_YEAR)"-" \
105
+ MRB_STRINGIZE(MRUBY_RELEASE_YEAR)" " \
106
+ MRUBY_AUTHOR \
107
+
108
+ MRB_END_DECL
109
+
110
+ #endif /* MRUBY_VERSION_H */
@@ -0,0 +1,317 @@
1
+ module Prism
2
+ @@instances = {}
3
+
4
+ def self.instances
5
+ @@instances
6
+ end
7
+
8
+ def self.mount(component)
9
+ Mount.new(component)
10
+ end
11
+
12
+ class Mount
13
+ def initialize(component)
14
+ @component = component
15
+ end
16
+
17
+ def render
18
+ JSON::stringify(@component.render)
19
+ end
20
+
21
+ def dispatch(messageJSON)
22
+ message = JSON::parse(messageJSON)
23
+
24
+ instance = Prism.instances[message["instance"]]
25
+
26
+ if instance.respond_to? message["type"]
27
+ instance.send(message["type"], *message["args"])
28
+ else
29
+ raise "Component #{instance.class} has no method ##{message["type"]}"
30
+ end
31
+ end
32
+
33
+ def event(eventJSON, id)
34
+ DOM.event(JSON::parse(eventJSON), id)
35
+ end
36
+ end
37
+
38
+ class Component
39
+ TAG_NAMES = [
40
+ 'a',
41
+ 'abbr',
42
+ 'address',
43
+ 'area',
44
+ 'article',
45
+ 'aside',
46
+ 'audio',
47
+ 'b',
48
+ 'base',
49
+ 'bdi',
50
+ 'bdo',
51
+ 'blockquote',
52
+ 'body',
53
+ 'br',
54
+ 'button',
55
+ 'canvas',
56
+ 'caption',
57
+ 'cite',
58
+ 'code',
59
+ 'col',
60
+ 'colgroup',
61
+ 'dd',
62
+ 'del',
63
+ 'details',
64
+ 'dfn',
65
+ 'dir',
66
+ 'div',
67
+ 'dl',
68
+ 'dt',
69
+ 'em',
70
+ 'embed',
71
+ 'fieldset',
72
+ 'figcaption',
73
+ 'figure',
74
+ 'footer',
75
+ 'form',
76
+ 'h1',
77
+ 'h2',
78
+ 'h3',
79
+ 'h4',
80
+ 'h5',
81
+ 'h6',
82
+ 'head',
83
+ 'header',
84
+ 'hgroup',
85
+ 'hr',
86
+ 'html',
87
+ 'i',
88
+ 'iframe',
89
+ 'img',
90
+ 'input',
91
+ 'ins',
92
+ 'kbd',
93
+ 'keygen',
94
+ 'label',
95
+ 'legend',
96
+ 'li',
97
+ 'link',
98
+ 'main',
99
+ 'map',
100
+ 'mark',
101
+ 'menu',
102
+ 'meta',
103
+ 'nav',
104
+ 'noscript',
105
+ 'object',
106
+ 'ol',
107
+ 'optgroup',
108
+ 'option',
109
+ 'p',
110
+ 'param',
111
+ 'pre',
112
+ 'progress',
113
+ 'q',
114
+ 'rp',
115
+ 'rt',
116
+ 'ruby',
117
+ 's',
118
+ 'samp',
119
+ 'script',
120
+ 'section',
121
+ 'select',
122
+ 'small',
123
+ 'source',
124
+ 'span',
125
+ 'strong',
126
+ 'style',
127
+ 'sub',
128
+ 'summary',
129
+ 'sup',
130
+ 'table',
131
+ 'tbody',
132
+ 'td',
133
+ 'textarea',
134
+ 'tfoot',
135
+ 'th',
136
+ 'thead',
137
+ 'time',
138
+ 'title',
139
+ 'tr',
140
+ 'u',
141
+ 'ul',
142
+ 'video',
143
+ ]
144
+
145
+ TAG_NAMES.each do |tag|
146
+ define_method(tag.to_sym) do |*args|
147
+ options = {}
148
+ className = ""
149
+ children = []
150
+
151
+ result = {}
152
+
153
+ until args.empty?
154
+ arg = args.shift
155
+
156
+ case arg
157
+ when String
158
+ if arg.start_with?(".")
159
+ className = arg
160
+ else
161
+ children = [text(arg)]
162
+ end
163
+ when Array
164
+ children = arg
165
+ when Object
166
+ options = arg
167
+ end
168
+ end
169
+
170
+ options.each do |key, value|
171
+ next if value.is_a?(EventHandler) || (key.to_s).chars.first == '_'
172
+ result[key] = value
173
+ end
174
+
175
+ result[:_type] = tag
176
+ result[:_class] = className
177
+ options[:_children] = children || []
178
+
179
+ result[:_children] = options[:_children].compact.map do |child|
180
+ if child.is_a?(Prism::Component)
181
+ child.render
182
+ elsif child.is_a?(String)
183
+ text(child)
184
+ else
185
+ child
186
+ end
187
+ end
188
+
189
+ result[:on] ||= {}
190
+ result[:on][:click] = options[:onClick].to_hash if options[:onClick]
191
+ result[:on][:change] = options[:onChange].to_hash if options[:onChange]
192
+ result[:on][:input] = options[:onInput].to_hash if options[:onInput]
193
+ result[:on][:mousedown] = options[:onMousedown].to_hash if options[:onMousedown]
194
+ result[:on][:mouseup] = options[:onMouseup].to_hash if options[:onMouseup]
195
+ result[:on][:keydown] = options[:onKeydown].to_hash if options[:onKeydown]
196
+ result[:on][:keyup] = options[:onKeyup].to_hash if options[:onKeyup]
197
+ result[:on][:scroll] = options[:onScroll].to_hash if options[:onScroll]
198
+
199
+ if options[:on]
200
+ event_handlers = {}
201
+
202
+ options[:on].each do |key, value|
203
+ event_handlers[key] = value.to_hash
204
+ end
205
+
206
+ result[:on] = event_handlers
207
+ end
208
+
209
+ result
210
+ end
211
+ end
212
+
213
+ def text(t)
214
+ {:type => "text", :content => t.to_s}
215
+ end
216
+
217
+ def call(method_name, *args)
218
+ Prism.instances[object_id] = self # TODO - this is a memory leak
219
+ EventHandler.new(object_id, method_name).with(*args)
220
+ end
221
+
222
+ def stop_propagation
223
+ Prism.instances[object_id] = self # TODO - this is a memory leak
224
+ EventHandler.new(object_id, nil).stop_propagation
225
+ end
226
+
227
+ def prevent_default
228
+ Prism.instances[object_id] = self # TODO - this is a memory leak
229
+ EventHandler.new(object_id, nil).prevent_default
230
+ end
231
+
232
+ def render
233
+ raise "Unimplemented render method for #{self.class.name}"
234
+ end
235
+ end
236
+
237
+ class EventHandler
238
+ attr_reader :method_name
239
+
240
+ def initialize(id, method_name, args = [], options = {})
241
+ @id = id
242
+ @method_name = method_name
243
+ @args = args
244
+ @options = {prevent_default: false, stop_propagation: false}.merge(options)
245
+ end
246
+
247
+ def with(*additionalArgs)
248
+ new_args = additionalArgs.map { |a| {type: :constant, value: a} }
249
+
250
+ EventHandler.new(@id, method_name, @args + new_args, @options)
251
+ end
252
+
253
+ def with_event_data(*property_names)
254
+ new_args = property_names.map { |item| {type: :event_data, key: item } }
255
+
256
+ EventHandler.new(@id, method_name, @args + new_args, @options)
257
+ end
258
+
259
+ def with_target_data(*items)
260
+ target_args = items.map { |item| {type: :target_data, key: item } }
261
+ EventHandler.new(@id, method_name, @args + target_args, @options)
262
+ end
263
+
264
+ def prevent_default
265
+ EventHandler.new(@id, method_name, @args, @options.merge(prevent_default: true))
266
+ end
267
+
268
+ def stop_propagation
269
+ EventHandler.new(@id, method_name, @args, @options.merge(stop_propagation: true))
270
+ end
271
+
272
+ def to_hash
273
+ {
274
+ instance: @id,
275
+ type: @method_name,
276
+ args: @args,
277
+ prevent_default: @options[:prevent_default],
278
+ stop_propagation: @options[:stop_propagation]
279
+ }
280
+ end
281
+ end
282
+ end
283
+
284
+ module DOM
285
+ @@event_id = 0
286
+ @@listeners = {}
287
+
288
+ def self.get_event_id
289
+ @@event_id += 1
290
+
291
+ @@event_id.to_s
292
+ end
293
+
294
+ def self.add_listener(id, &block)
295
+ @@listeners[id] = block
296
+ end
297
+
298
+ def self.select(selector)
299
+ ElementSelection.new(selector)
300
+ end
301
+
302
+ def self.event(eventData, id)
303
+ @@listeners[id].call(eventData)
304
+ end
305
+
306
+ class ElementSelection
307
+ def initialize(selector)
308
+ @selector = selector
309
+ end
310
+
311
+ def on(eventName, &block)
312
+ id = DOM.get_event_id
313
+ InternalDOM.add_event_listener(@selector, eventName, id)
314
+ DOM.add_listener(id, &block)
315
+ end
316
+ end
317
+ end
@@ -0,0 +1,23 @@
1
+ const http = require('http');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+
5
+ const proxy = http.createServer((req, res) => {
6
+ console.log(req.url);
7
+ const p = path.join('.', req.url);
8
+
9
+ try {
10
+ if (p.endsWith('.wasm')) {
11
+ res.setHeader("Content-Type", "application/wasm")
12
+ }
13
+ if (p.endsWith('.js')) {
14
+ res.setHeader("Content-Type", "text/javascript")
15
+ }
16
+ res.write(fs.readFileSync(p));
17
+ } catch (e) {
18
+ res.write(e.toString());
19
+ }
20
+ res.end();
21
+ });
22
+
23
+ proxy.listen(1337, '127.0.0.1');