AmberVM 0.0.19 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/ambervm +8 -7
- data/lib/amber.rb +1 -1
- data/lib/amber/classes/object.rb +3 -2
- data/lib/amber/environment.rb +11 -13
- data/lib/amber/interpreter.rb +40 -24
- data/lib/amber/languages.rb +1 -1
- data/lib/amber/languages/ecma/compiler.rb +77 -64
- data/spec/languages/ecma/ecma_objects_spec.rb +13 -11
- data/spec/languages/ecma/ecma_old_spec.rb +2 -2
- metadata +2 -10
- data/spec/languages/ecma_fuku/ecma_array_spec.rb +0 -61
- data/spec/languages/ecma_fuku/ecma_closure_spec.rb +0 -33
- data/spec/languages/ecma_fuku/ecma_function_spec.rb +0 -84
- data/spec/languages/ecma_fuku/ecma_literals_spec.rb +0 -55
- data/spec/languages/ecma_fuku/ecma_objects_spec.rb +0 -133
- data/spec/languages/ecma_fuku/ecma_old_spec.rb +0 -415
- data/spec/languages/ecma_fuku/ecma_operator_spec.rb +0 -33
- data/spec/languages/ecma_fuku/ecma_spec.rb +0 -52
@@ -1,415 +0,0 @@
|
|
1
|
-
$: << 'lib'
|
2
|
-
require 'amber'
|
3
|
-
require 'amber/languages'
|
4
|
-
require 'amber/languages/ecma_fuku'
|
5
|
-
|
6
|
-
describe AmberVM::Languages::ECMA_Fuku do
|
7
|
-
before(:each) do
|
8
|
-
@compiler = AmberVM::Languages::ECMA_Fuku.new
|
9
|
-
@env = {}
|
10
|
-
end
|
11
|
-
|
12
|
-
def run code
|
13
|
-
@compiler.compile(code).execute(@env)
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "literals" do
|
17
|
-
describe "strings" do
|
18
|
-
it "should handle string literals" do
|
19
|
-
res = run <<-EOC
|
20
|
-
"Hallo!"
|
21
|
-
EOC
|
22
|
-
res.should == "Hallo!"
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should handle newline replacements" do
|
26
|
-
res = run '"\n"'
|
27
|
-
res.should == "\n"
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should handle newline replacements" do
|
31
|
-
res = run <<-'EOC'
|
32
|
-
"\t"
|
33
|
-
EOC
|
34
|
-
res.should == "\t"
|
35
|
-
end
|
36
|
-
it "should handle newline replacements" do
|
37
|
-
res = run <<-'EOC'
|
38
|
-
"\n"
|
39
|
-
EOC
|
40
|
-
res.should == "\n"
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should handle double escapes" do
|
44
|
-
res = run <<-'EOC'
|
45
|
-
"\\t"
|
46
|
-
EOC
|
47
|
-
res.should == "\\t"
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should handle double escapes followed by single escapes" do
|
51
|
-
res = run <<-'EOF'
|
52
|
-
"\\\t"
|
53
|
-
EOF
|
54
|
-
res.should == "\\\t"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should handle number literals" do
|
59
|
-
res = run <<-EOC
|
60
|
-
42
|
61
|
-
EOC
|
62
|
-
res.should == 42
|
63
|
-
end
|
64
|
-
|
65
|
-
describe "array" do
|
66
|
-
it "should handle array literals with no elements" do
|
67
|
-
res = run <<-EOC
|
68
|
-
[]
|
69
|
-
EOC
|
70
|
-
res.should == []
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should handle array literals with one element" do
|
74
|
-
res = run <<-EOC
|
75
|
-
["Kekse!"]
|
76
|
-
EOC
|
77
|
-
res.should == ["Kekse!"]
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should handle array literals with arrays in it" do
|
81
|
-
res = run <<-EOC
|
82
|
-
[["Kekse!",2],1]
|
83
|
-
EOC
|
84
|
-
res.should == [["Kekse!",2],1]
|
85
|
-
end
|
86
|
-
|
87
|
-
it "should handle array literals with multiple elements" do
|
88
|
-
res = run <<-EOC
|
89
|
-
[42, "Kekse!"]
|
90
|
-
EOC
|
91
|
-
res.should == [42, "Kekse!"]
|
92
|
-
end
|
93
|
-
|
94
|
-
it "should allow to set array elements" do
|
95
|
-
res = run <<-EOC
|
96
|
-
array = [];
|
97
|
-
array[0] = 42;
|
98
|
-
array;
|
99
|
-
EOC
|
100
|
-
res.should == [42]
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
describe "Objects array" do
|
105
|
-
it "should handle an empty object" do
|
106
|
-
res = run <<-EOC
|
107
|
-
a = {}
|
108
|
-
EOC
|
109
|
-
@env['a'].should be_instance_of(OpenStruct)
|
110
|
-
end
|
111
|
-
|
112
|
-
it "should handle an object with variables" do
|
113
|
-
res = run <<-EOC
|
114
|
-
{a: 1}
|
115
|
-
EOC
|
116
|
-
res.should be_instance_of(OpenStruct)
|
117
|
-
res.a.should == 1
|
118
|
-
end
|
119
|
-
it "should handle an object with functions" do
|
120
|
-
res = run <<-EOC
|
121
|
-
{f: function(){1;}}
|
122
|
-
EOC
|
123
|
-
res.should be_instance_of(OpenStruct)
|
124
|
-
res.f.should be
|
125
|
-
res.f.call.should == :undefined
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
it "should handle true literals" do
|
130
|
-
res = run <<-EOC
|
131
|
-
true
|
132
|
-
EOC
|
133
|
-
res.should be_true
|
134
|
-
end
|
135
|
-
|
136
|
-
it "should handle false literals" do
|
137
|
-
res = run <<-EOC
|
138
|
-
false
|
139
|
-
EOC
|
140
|
-
res.should_not be_true
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
describe "array" do
|
145
|
-
it "should allow accessing elements" do
|
146
|
-
@env['a'] = [1,2,3]
|
147
|
-
res = run <<-EOC
|
148
|
-
a[0]
|
149
|
-
EOC
|
150
|
-
res.should == 1
|
151
|
-
end
|
152
|
-
|
153
|
-
it "should allow setting elements" do
|
154
|
-
@env['a'] = [1,2,3]
|
155
|
-
res = run <<-EOC
|
156
|
-
a[0] = 42;
|
157
|
-
EOC
|
158
|
-
res.should == 42
|
159
|
-
@env['a'][0].should == 42
|
160
|
-
end
|
161
|
-
it "should allow accessing the length" do
|
162
|
-
@env['a'] = [1,2,3]
|
163
|
-
res = run <<-EOC
|
164
|
-
a.length
|
165
|
-
EOC
|
166
|
-
res.should == 3
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
describe "variables" do
|
171
|
-
it "should recognize simple assignments" do
|
172
|
-
res = run <<-EOC
|
173
|
-
variable = 'value';
|
174
|
-
EOC
|
175
|
-
res.should == 'value'
|
176
|
-
@env['variable'].should == 'value'
|
177
|
-
end
|
178
|
-
|
179
|
-
it "should recognize local assignments" do
|
180
|
-
res = run <<-EOC
|
181
|
-
var variable = 'value';
|
182
|
-
EOC
|
183
|
-
res.should == 'value'
|
184
|
-
@env['variable'].should == 'value'
|
185
|
-
end
|
186
|
-
|
187
|
-
it "should recognize multiple local assignments" do
|
188
|
-
res = run <<-EOC
|
189
|
-
var i = 'a', j = 'b';
|
190
|
-
EOC
|
191
|
-
res.should == 'b'
|
192
|
-
@env['i'].should == 'a'
|
193
|
-
@env['j'].should == 'b'
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should scope global assignments correctly" do
|
197
|
-
res = run <<-EOC
|
198
|
-
variable = 'outer scope';
|
199
|
-
{
|
200
|
-
var variable = 'inner scope';
|
201
|
-
}
|
202
|
-
EOC
|
203
|
-
res.should == 'inner scope'
|
204
|
-
@env['variable'].should == 'inner scope'
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
describe "conditions" do
|
209
|
-
describe "true condition" do
|
210
|
-
describe "without blocks" do
|
211
|
-
|
212
|
-
it "should handle loops without else" do
|
213
|
-
res = run <<-EOC
|
214
|
-
if (true)
|
215
|
-
42
|
216
|
-
EOC
|
217
|
-
res.should == 42
|
218
|
-
end
|
219
|
-
|
220
|
-
it "should handle loops with else" do
|
221
|
-
res = run <<-EOC
|
222
|
-
if (true)
|
223
|
-
42;
|
224
|
-
else
|
225
|
-
23
|
226
|
-
EOC
|
227
|
-
res.should == 42
|
228
|
-
end
|
229
|
-
|
230
|
-
it "should handle loops with else and ugly ';'" do
|
231
|
-
res = run <<-EOC
|
232
|
-
if (true)
|
233
|
-
42;
|
234
|
-
else
|
235
|
-
23
|
236
|
-
EOC
|
237
|
-
res.should == 42
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
describe "with blocks" do
|
242
|
-
it "should handle condition without else" do
|
243
|
-
res = run <<-EOC
|
244
|
-
if (true) {
|
245
|
-
42;
|
246
|
-
}
|
247
|
-
EOC
|
248
|
-
res.should == 42
|
249
|
-
end
|
250
|
-
it "should handle condition with else" do
|
251
|
-
res = run <<-EOC
|
252
|
-
if (true) {
|
253
|
-
42;
|
254
|
-
} else {
|
255
|
-
23;
|
256
|
-
}
|
257
|
-
EOC
|
258
|
-
res.should == 42
|
259
|
-
end
|
260
|
-
end
|
261
|
-
end
|
262
|
-
describe "false condition" do
|
263
|
-
describe "without blocks" do
|
264
|
-
it "should handle condition without else" do
|
265
|
-
res = run <<-EOC
|
266
|
-
if (false)
|
267
|
-
42;
|
268
|
-
EOC
|
269
|
-
res.should == nil
|
270
|
-
end
|
271
|
-
it "should handle condition with else" do
|
272
|
-
res = run <<-EOC
|
273
|
-
if (false)
|
274
|
-
42;
|
275
|
-
else
|
276
|
-
23;
|
277
|
-
EOC
|
278
|
-
res.should == 23
|
279
|
-
end
|
280
|
-
end
|
281
|
-
describe "with blocks" do
|
282
|
-
it "should handle condition without else" do
|
283
|
-
res = run <<-EOC
|
284
|
-
if (false) {
|
285
|
-
42;
|
286
|
-
}
|
287
|
-
EOC
|
288
|
-
res.should be_nil
|
289
|
-
end
|
290
|
-
it "should handle condition with else" do
|
291
|
-
res = run <<-EOC
|
292
|
-
if (false) {
|
293
|
-
42;
|
294
|
-
} else {
|
295
|
-
23;
|
296
|
-
}
|
297
|
-
EOC
|
298
|
-
res.should == 23
|
299
|
-
end
|
300
|
-
end
|
301
|
-
end
|
302
|
-
end
|
303
|
-
|
304
|
-
describe "loops" do
|
305
|
-
it "should handle for loops" do
|
306
|
-
res = run <<-EOC
|
307
|
-
variable = "";
|
308
|
-
for (i = 0; i <= 3; i = i + 1) {
|
309
|
-
variable = variable + i;
|
310
|
-
}
|
311
|
-
EOC
|
312
|
-
@env['variable'].should == '0123'
|
313
|
-
end
|
314
|
-
|
315
|
-
it "should handle for loops (bug prevention)" do
|
316
|
-
code = <<-EOC
|
317
|
-
function test() {for (var i = 1; i <= 2; i+=1) {};};
|
318
|
-
test();
|
319
|
-
EOC
|
320
|
-
proc { run(code) }.should_not raise_error
|
321
|
-
end
|
322
|
-
|
323
|
-
it "should handle while loops" do
|
324
|
-
res = run <<-EOC
|
325
|
-
variable = "";
|
326
|
-
i = 0;
|
327
|
-
while (i < 3) {
|
328
|
-
i = i + 1;
|
329
|
-
variable = variable + i;
|
330
|
-
}
|
331
|
-
EOC
|
332
|
-
@env['variable'].should == '123'
|
333
|
-
end
|
334
|
-
|
335
|
-
it "should handle do while loops" do
|
336
|
-
res = run <<-EOC
|
337
|
-
variable = "";
|
338
|
-
i = 0;
|
339
|
-
do {
|
340
|
-
i = i + 1;
|
341
|
-
variable = variable + i;
|
342
|
-
} while (i < 3)
|
343
|
-
EOC
|
344
|
-
@env['variable'].should == '123'
|
345
|
-
end
|
346
|
-
end
|
347
|
-
it "should execute do while loops at least once" do
|
348
|
-
res = run <<-EOC
|
349
|
-
variable = "";
|
350
|
-
i = 0;
|
351
|
-
do {
|
352
|
-
i = i + 1;
|
353
|
-
variable = variable + i;
|
354
|
-
} while (i < 0)
|
355
|
-
EOC
|
356
|
-
res.should be_nil
|
357
|
-
@env['variable'].should == '1'
|
358
|
-
end
|
359
|
-
|
360
|
-
describe "objects" do
|
361
|
-
it "should handle object variables" do
|
362
|
-
obj = mock('object mock')
|
363
|
-
obj.should_receive(:test).with(no_args).and_return('ok')
|
364
|
-
@env['obj'] = obj
|
365
|
-
res = run <<-EOC
|
366
|
-
obj.test;
|
367
|
-
EOC
|
368
|
-
res.should == 'ok'
|
369
|
-
end
|
370
|
-
|
371
|
-
it "should handle object functions" do
|
372
|
-
obj = mock('object mock')
|
373
|
-
function = mock('function mock')
|
374
|
-
obj.should_receive(:test).with(no_args).and_return(function)
|
375
|
-
function.should_receive(:call).once.with(no_args).and_return('ok')
|
376
|
-
@env['obj'] = obj
|
377
|
-
res = run <<-EOC
|
378
|
-
obj.test();
|
379
|
-
EOC
|
380
|
-
res.should == 'ok'
|
381
|
-
end
|
382
|
-
end
|
383
|
-
|
384
|
-
|
385
|
-
describe "opperators" do
|
386
|
-
it "should recognize ++" do
|
387
|
-
@env['i'] = 41
|
388
|
-
res = run <<-EOC
|
389
|
-
i++;
|
390
|
-
EOC
|
391
|
-
res.should == 41
|
392
|
-
end
|
393
|
-
it "should recognize --" do
|
394
|
-
@env['i'] = 43
|
395
|
-
res = run <<-EOC
|
396
|
-
i--;
|
397
|
-
EOC
|
398
|
-
res.should == 43
|
399
|
-
end
|
400
|
-
it "should recognize +=" do
|
401
|
-
@env['i'] = 40
|
402
|
-
res = run <<-EOC
|
403
|
-
i += 2;
|
404
|
-
EOC
|
405
|
-
res.should == 42
|
406
|
-
end
|
407
|
-
it "should recognize -=" do
|
408
|
-
@env['i'] = 44
|
409
|
-
res = run <<-EOC
|
410
|
-
i -= 2;
|
411
|
-
EOC
|
412
|
-
res.should == 42
|
413
|
-
end
|
414
|
-
end
|
415
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
$: << 'lib'
|
2
|
-
require 'amber'
|
3
|
-
require 'amber/languages'
|
4
|
-
require 'amber/languages/ecma_fuku'
|
5
|
-
|
6
|
-
describe AmberVM::Languages::ECMA_Fuku do
|
7
|
-
before(:each) do
|
8
|
-
@env = {}
|
9
|
-
@compiler = AmberVM::Languages::ECMA_Fuku.new
|
10
|
-
end
|
11
|
-
|
12
|
-
def run code
|
13
|
-
@compiler.compile(code).execute(@env)
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "arithmetic" do
|
17
|
-
it "should handle >>>" do
|
18
|
-
result = run <<-CODE
|
19
|
-
-1766 >>> 12
|
20
|
-
CODE
|
21
|
-
result.should == 1048575
|
22
|
-
end
|
23
|
-
it "should handle >>>=" do
|
24
|
-
result = run <<-CODE
|
25
|
-
a = -8345;
|
26
|
-
a >>>= 4;
|
27
|
-
a
|
28
|
-
CODE
|
29
|
-
result.should == 268434934
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
$: << 'lib'
|
2
|
-
require 'amber'
|
3
|
-
require 'amber/languages'
|
4
|
-
require 'amber/languages/ecma_fuku'
|
5
|
-
|
6
|
-
describe AmberVM::Languages::ECMA_Fuku do
|
7
|
-
before(:each) do
|
8
|
-
@env = {}
|
9
|
-
@compiler = AmberVM::Languages::ECMA_Fuku.new
|
10
|
-
end
|
11
|
-
|
12
|
-
def run code
|
13
|
-
@compiler.compile(code).execute(@env)
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "variables" do
|
17
|
-
it "should get variables" do
|
18
|
-
@env['test'] = 42
|
19
|
-
result = run <<-CODE
|
20
|
-
test
|
21
|
-
CODE
|
22
|
-
result.should == 42
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should allow to assign literals to variables" do
|
26
|
-
result = run <<-CODE
|
27
|
-
test = 23
|
28
|
-
CODE
|
29
|
-
result.should == 23
|
30
|
-
@env['test'].should == 23
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should autocast strings when using <var>++" do
|
34
|
-
result = run <<-CODE
|
35
|
-
test = '1.2';
|
36
|
-
test++
|
37
|
-
CODE
|
38
|
-
result.should == 1.2
|
39
|
-
@env['test'].should == 2.2
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe '+ operator' do
|
44
|
-
it 'should behave like ECMA' do
|
45
|
-
result = run <<-CODE
|
46
|
-
1 + "3"
|
47
|
-
CODE
|
48
|
-
result.should == "13"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|