-
Notifications
You must be signed in to change notification settings - Fork 33
/
operation.h
624 lines (545 loc) · 19.4 KB
/
operation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#ifndef IV_LV5_RAILGUN_OPERATION_H_
#define IV_LV5_RAILGUN_OPERATION_H_
#include <iv/arith.h>
#include <iv/platform_math.h>
#include <iv/lv5/arguments.h>
#include <iv/lv5/jsval.h>
#include <iv/lv5/chain.h>
#include <iv/lv5/railgun/fwd.h>
#include <iv/lv5/railgun/instruction_fwd.h>
namespace iv {
namespace lv5 {
namespace railgun {
namespace detail {
template<int Target>
inline bool IsIncrementOverflowSafe(int32_t val);
template<>
inline bool IsIncrementOverflowSafe<-1>(int32_t val) {
return val > INT32_MIN;
}
template<>
inline bool IsIncrementOverflowSafe<1>(int32_t val) {
return val < INT32_MAX;
}
class VMArguments : public lv5::Arguments {
public:
VMArguments(lv5::Context* ctx, pointer ptr, std::size_t n)
: lv5::Arguments(ctx, ptr, n) {
}
};
} // namespace detail
class Operation {
public:
explicit Operation(lv5::Context* ctx) : ctx_(ctx) { }
JSVal Invoke(JSFunction* func, JSVal* arg, int argc_with_this, Error* e) {
detail::VMArguments args(ctx_,
arg + (argc_with_this - 1),
argc_with_this - 1);
return func->Call(&args, args.this_binding(), e);
}
JSVal InvokeMaybeEval(JSFunction* func,
JSVal* arg, int argc_with_this,
Frame* prev, Error* e) {
detail::VMArguments args(ctx_,
arg + (argc_with_this - 1),
argc_with_this - 1);
const JSAPI native = func->NativeFunction();
if (native && native == &GlobalEval) {
// direct call to eval point
args.set_this_binding(args.this_binding());
return DirectCallToEval(args, prev, e);
}
return func->Call(&args, args.this_binding(), e);
}
JSVal Construct(JSFunction* func, JSVal* arg, int argc_with_this, Error* e) {
detail::VMArguments args(ctx_,
arg + (argc_with_this - 1),
argc_with_this - 1);
args.set_constructor_call(true);
return func->Construct(&args, e);
}
void RaiseReferenceError(Symbol name, Error* e) const {
core::UStringBuilder builder;
builder.Append('"');
builder.Append(symbol::GetSymbolString(name));
builder.Append("\" not defined");
e->Report(Error::Reference, builder.BuildPiece());
}
void RaiseReferenceError(Error* e) const {
core::UStringBuilder builder;
builder.Append("Invalid left-hand side expression");
e->Report(Error::Reference, builder.BuildPiece());
}
JSEnv* GetEnv(JSEnv* env, Symbol name) const {
JSEnv* current = env;
while (current) {
if (current->HasBinding(ctx_, name)) {
return current;
} else {
current = current->outer();
}
}
return NULL;
}
JSDeclEnv* GetHeapEnv(JSEnv* env, uint32_t scope_nest_count) const {
for (uint32_t i = 0; i < scope_nest_count; ++i) {
env = env->outer();
}
assert(env->AsJSDeclEnv());
return static_cast<JSDeclEnv*>(env);
}
#define CHECK IV_LV5_ERROR_WITH(e, JSEmpty)
JSVal LoadName(JSEnv* env, Symbol name, bool strict, Error* e) {
if (JSEnv* current = GetEnv(env, name)) {
return current->GetBindingValue(ctx_, name, strict, e);
}
RaiseReferenceError(name, e);
return JSEmpty;
}
JSVal LoadHeap(JSEnv* env, Symbol name,
bool strict, uint32_t offset,
uint32_t scope_nest_count, Error* e) {
return GetHeapEnv(env, scope_nest_count)->GetByOffset(offset, strict, e);
}
JSVal LoadProp(JSVal base, Symbol s, bool strict, Error* e) {
base.CheckObjectCoercible(CHECK);
return LoadPropImpl(base, s, strict, e);
}
template<OP::Type own, OP::Type proto, OP::Type chain, OP::Type generic>
JSVal LoadProp(Instruction* instr,
JSVal base, Symbol s, bool strict, Error* e) {
// opcode | (dst | base | name) | nop | nop | nop
base.CheckObjectCoercible(CHECK);
Slot slot;
const JSVal res = base.GetSlot(ctx_, s, &slot, IV_LV5_ERROR(e));
if (slot.IsNotFound()) {
return res;
}
// property found
if (!slot.IsLoadCacheable() || symbol::IsArrayIndexSymbol(s)) {
// bailout to generic
instr[0] = Instruction::GetOPInstruction(generic);
return slot.Get(ctx_, base, e);
}
assert(!symbol::IsArrayIndexSymbol(s));
JSObject* obj = NULL;
if (base.IsPrimitive()) {
// if base is primitive, property not found in "this" object
// so, lookup from proto
obj = base.GetPrimitiveProto(ctx_);
} else {
obj = base.object();
}
// cache phase
// own property / proto property / chain lookup property
if (slot.base() == obj) {
// own property
instr[0] = Instruction::GetOPInstruction(own);
instr[2].map = obj->map();
instr[3].u32[0] = slot.offset();
return slot.value();
}
if (slot.base() == obj->prototype()) {
// proto property
obj->FlattenMap();
instr[0] = Instruction::GetOPInstruction(proto);
instr[2].map = obj->map();
instr[3].map = slot.base()->map();
instr[4].u32[0] = slot.offset();
return slot.value();
}
// chain property
instr[0] = Instruction::GetOPInstruction(chain);
instr[2].chain = Chain::New(obj, slot.base());
instr[3].map = slot.base()->map();
instr[4].u32[0] = slot.offset();
return slot.value();
}
JSVal LoadElement(JSVal base,
JSVal element, bool strict, Error* e) {
base.CheckObjectCoercible(CHECK);
const Symbol s = element.ToSymbol(ctx_, CHECK);
return LoadPropImpl(base, s, strict, e);
}
JSVal LoadPropImpl(JSVal base,
Symbol s, bool strict, Error* e) {
if (base.IsPrimitive()) {
return LoadPropPrimitive(base, s, strict, e);
} else {
return base.object()->Get(ctx_, s, e);
}
}
JSVal LoadPropPrimitive(JSVal base,
Symbol s, bool strict, Error* e) {
JSVal res;
if (GetPrimitiveOwnProperty(base, s, &res)) {
return res;
}
// if base is primitive, property not found in "this" object
// so, lookup from proto
Slot slot;
JSObject* const proto = base.GetPrimitiveProto(ctx_);
if (proto->GetPropertySlot(ctx_, s, &slot)) {
return slot.Get(ctx_, base, e);
} else {
return JSUndefined;
}
}
#undef CHECK
#define CHECK IV_LV5_ERROR_VOID(e)
void StoreName(JSEnv* env, Symbol name,
JSVal stored, bool strict, Error* e) {
if (JSEnv* current = GetEnv(env, name)) {
current->SetMutableBinding(ctx_, name, stored, strict, e);
} else {
if (strict) {
e->Report(Error::Reference,
"putting to unresolvable reference "
"not allowed in strict reference");
} else {
ctx_->global_obj()->Put(ctx_, name, stored, strict, e);
}
}
}
void StoreHeap(JSEnv* env, Symbol name,
JSVal stored, bool strict,
uint32_t offset, uint32_t scope_nest_count, Error* e) {
GetHeapEnv(env, scope_nest_count)->SetByOffset(offset, stored, strict, e);
}
void StoreElement(JSVal base, JSVal element,
JSVal stored, bool strict, Error* e) {
base.CheckObjectCoercible(CHECK);
const Symbol s = element.ToSymbol(ctx_, CHECK);
StorePropImpl(base, s, stored, strict, e);
}
void StoreProp(JSVal base, Symbol s,
JSVal stored, bool strict, Error* e) {
base.CheckObjectCoercible(CHECK);
StorePropImpl(base, s, stored, strict, e);
}
void StoreProp(JSVal base,
Instruction* instr,
OP::Type generic,
Symbol name, JSVal src, bool strict, Error* e) {
// opcode | (base | src | index) | nop | nop
base.CheckObjectCoercible(CHECK);
if (base.IsPrimitive()) {
StorePropPrimitive(base, name, src, strict, e);
return;
}
// cache patten
JSObject* obj = base.object();
if (instr[2].map == obj->map()) {
// map is cached, so use previous index code
obj->Direct(instr[3].u32[0]) = src;
return;
}
Map* previous = obj->map();
Slot slot;
obj->PutSlot(ctx(), name, src, &slot, strict, CHECK);
const Slot::PutResultType put_result_type = slot.put_result_type();
// uncacheable pattern
if (!slot.IsPutCacheable() || !symbol::IsArrayIndexSymbol(name)) {
return;
}
assert(put_result_type != Slot::PUT_NONE);
// TODO(Constellation) VM only store replace pattern.
if (put_result_type == Slot::PUT_REPLACE) {
if (previous == obj->map()) {
instr[2].map = obj->map();
instr[3].u32[0] = slot.offset();
}
}
return;
}
void StorePropImpl(JSVal base, Symbol s,
JSVal stored, bool strict, Error* e) {
if (base.IsPrimitive()) {
StorePropPrimitive(base, s, stored, strict, e);
} else {
base.object()->Put(ctx_, s, stored, strict, e);
}
}
void StorePropPrimitive(JSVal base, Symbol s,
JSVal stored, bool strict, Error* e) {
assert(base.IsPrimitive());
Slot slot;
JSObject* const o = base.ToObject(ctx_, CHECK);
if (!o->CanPut(ctx_, s, &slot)) {
if (strict) {
e->Report(Error::Type, "cannot put value to object");
}
return;
}
if (slot.IsNotFound() || slot.attributes().IsData()) {
if (strict) {
e->Report(Error::Type, "value to symbol in transient object");
}
return;
}
const Accessor* ac = slot.accessor();
assert(ac->setter());
ScopedArguments args(ctx_, 1, IV_LV5_ERROR_VOID(e));
args[0] = stored;
static_cast<JSFunction*>(ac->setter())->Call(&args, base, e);
}
#undef CHECK
#define CHECK IV_LV5_ERROR_WITH(e, JSEmpty)
JSVal BinaryAdd(JSVal lhs, JSVal rhs, Error* e) const {
if (lhs.IsNumber() && rhs.IsNumber()) {
return lhs.number() + rhs.number();
}
if (lhs.IsString()) {
if (rhs.IsString()) {
return JSString::New(ctx_, lhs.string(), rhs.string(), e);
} else {
const JSVal rp = rhs.ToPrimitive(ctx_, Hint::NONE, CHECK);
JSString* const rs = rp.ToString(ctx_, CHECK);
return JSString::New(ctx_, lhs.string(), rs, e);
}
}
const JSVal lprim = lhs.ToPrimitive(ctx_, Hint::NONE, CHECK);
const JSVal rprim = rhs.ToPrimitive(ctx_, Hint::NONE, CHECK);
if (lprim.IsString() || rprim.IsString()) {
JSString* const lstr = lprim.ToString(ctx_, CHECK);
JSString* const rstr = rprim.ToString(ctx_, CHECK);
return JSString::New(ctx_, lstr, rstr, e);
}
const double left = lprim.ToNumber(ctx_, CHECK);
return left + rprim.ToNumber(ctx_, e);
}
JSVal BinarySub(JSVal lhs, JSVal rhs, Error* e) const {
const double left = lhs.ToNumber(ctx_, CHECK);
return left - rhs.ToNumber(ctx_, e);
}
JSVal BinaryMultiply(JSVal lhs, JSVal rhs, Error* e) const {
const double left = lhs.ToNumber(ctx_, CHECK);
return left * rhs.ToNumber(ctx_, e);
}
JSVal BinaryDivide(JSVal lhs, JSVal rhs, Error* e) const {
const double left = lhs.ToNumber(ctx_, CHECK);
return left / rhs.ToNumber(ctx_, e);
}
JSVal BinaryModulo(JSVal lhs, JSVal rhs, Error* e) const {
const double left = lhs.ToNumber(ctx_, CHECK);
return core::math::Modulo(left, rhs.ToNumber(ctx_, e));
}
JSVal BinaryLShift(JSVal lhs, JSVal rhs, Error* e) const {
const int32_t left = lhs.ToInt32(ctx_, CHECK);
return JSVal::Int32(left << (rhs.ToInt32(ctx_, e) & 0x1f));
}
JSVal BinaryRShift(JSVal lhs, JSVal rhs, Error* e) const {
const int32_t left = lhs.ToInt32(ctx_, CHECK);
return JSVal::Int32(left >> (rhs.ToInt32(ctx_, e) & 0x1f));
}
JSVal BinaryRShiftLogical(JSVal lhs,
JSVal rhs, Error* e) const {
const uint32_t left = lhs.ToUInt32(ctx_, CHECK);
return JSVal::UInt32(left >> (rhs.ToInt32(ctx_, e) & 0x1f));
}
bool BinaryCompareLT(JSVal lhs,
JSVal rhs, Error* e) const {
return JSVal::Compare<true>(ctx_, lhs, rhs, e) == CMP_TRUE;
}
bool BinaryCompareLTE(JSVal lhs,
JSVal rhs, Error* e) const {
return JSVal::Compare<false>(ctx_, rhs, lhs, e) == CMP_FALSE;
}
bool BinaryCompareGT(JSVal lhs,
JSVal rhs, Error* e) const {
return JSVal::Compare<false>(ctx_, rhs, lhs, e) == CMP_TRUE;
}
bool BinaryCompareGTE(JSVal lhs,
JSVal rhs, Error* e) const {
return JSVal::Compare<true>(ctx_, lhs, rhs, e) == CMP_FALSE;
}
bool BinaryInstanceof(JSVal lhs,
JSVal rhs, Error* e) const {
if (!rhs.IsObject()) {
e->Report(Error::Type, "instanceof requires object");
return false;
}
JSObject* const robj = rhs.object();
if (!robj->IsCallable()) {
e->Report(Error::Type, "instanceof requires constructor");
return false;
}
return static_cast<JSFunction*>(robj)->HasInstance(ctx_, lhs, e);
}
bool BinaryIn(JSVal lhs,
JSVal rhs, Error* e) const {
if (!rhs.IsObject()) {
e->Report(Error::Type, "in requires object");
return false;
}
const Symbol s = lhs.ToSymbol(ctx_, IV_LV5_ERROR_WITH(e, false));
return rhs.object()->HasProperty(ctx_, s);
}
bool BinaryEqual(JSVal lhs,
JSVal rhs, Error* e) const {
return JSVal::AbstractEqual(ctx_, lhs, rhs, e);
}
bool BinaryStrictEqual(JSVal lhs,
JSVal rhs) const {
return JSVal::StrictEqual(lhs, rhs);
}
bool BinaryNotEqual(JSVal lhs,
JSVal rhs, Error* e) const {
return !JSVal::AbstractEqual(ctx_, lhs, rhs, e);
}
bool BinaryStrictNotEqual(JSVal lhs,
JSVal rhs) const {
return !JSVal::StrictEqual(lhs, rhs);
}
int32_t BinaryBitAnd(JSVal lhs,
JSVal rhs, Error* e) const {
const int32_t left = lhs.ToInt32(ctx_, IV_LV5_ERROR_WITH(e, 0));
return left & rhs.ToInt32(ctx_, e);
}
int32_t BinaryBitXor(JSVal lhs,
JSVal rhs, Error* e) const {
const int32_t left = lhs.ToInt32(ctx_, IV_LV5_ERROR_WITH(e, 0));
return left ^ rhs.ToInt32(ctx_, e);
}
int32_t BinaryBitOr(JSVal lhs,
JSVal rhs, Error* e) const {
const int32_t left = lhs.ToInt32(ctx_, IV_LV5_ERROR_WITH(e, 0));
return left | rhs.ToInt32(ctx_, e);
}
template<int Target, std::size_t Returned>
JSVal IncrementName(JSEnv* env, Symbol s, bool strict, Error* e) {
if (JSEnv* current = GetEnv(env, s)) {
const JSVal w = current->GetBindingValue(ctx_, s, strict, CHECK);
if (w.IsInt32() && detail::IsIncrementOverflowSafe<Target>(w.int32())) {
std::tuple<JSVal, JSVal> results;
const int32_t target = w.int32();
std::get<0>(results) = w;
std::get<1>(results) = JSVal::Int32(target + Target);
current->SetMutableBinding(
ctx_, s,
std::get<1>(results), strict, e);
return std::get<Returned>(results);
} else {
std::tuple<double, double> results;
std::get<0>(results) = w.ToNumber(ctx_, CHECK);
std::get<1>(results) = std::get<0>(results) + Target;
current->SetMutableBinding(ctx_, s,
std::get<1>(results), strict, e);
return std::get<Returned>(results);
}
}
RaiseReferenceError(s, e);
return 0.0;
}
template<int Target, std::size_t Returned>
JSVal IncrementHeap(JSEnv* env, Symbol s,
bool strict, uint32_t offset,
uint32_t scope_nest_count, Error* e) {
JSDeclEnv* decl = GetHeapEnv(env, scope_nest_count);
const JSVal w = decl->GetByOffset(offset, strict, CHECK);
if (w.IsInt32() && detail::IsIncrementOverflowSafe<Target>(w.int32())) {
std::tuple<JSVal, JSVal> results;
const int32_t target = w.int32();
std::get<0>(results) = w;
std::get<1>(results) = JSVal::Int32(target + Target);
decl->SetByOffset(offset, std::get<1>(results), strict, e);
return std::get<Returned>(results);
} else {
std::tuple<double, double> results;
std::get<0>(results) = w.ToNumber(ctx_, CHECK);
std::get<1>(results) = std::get<0>(results) + Target;
decl->SetByOffset(offset, std::get<1>(results), strict, e);
return std::get<Returned>(results);
}
}
template<int Target, std::size_t Returned>
JSVal IncrementElement(JSVal base,
JSVal element, bool strict, Error* e) {
base.CheckObjectCoercible(CHECK);
const Symbol s = element.ToSymbol(ctx_, CHECK);
const JSVal w = LoadPropImpl(base, s, strict, CHECK);
if (w.IsInt32() && detail::IsIncrementOverflowSafe<Target>(w.int32())) {
std::tuple<JSVal, JSVal> results;
const int32_t target = w.int32();
std::get<0>(results) = w;
std::get<1>(results) = JSVal::Int32(target + Target);
StorePropImpl(base, s, std::get<1>(results), strict, e);
return std::get<Returned>(results);
} else {
std::tuple<double, double> results;
std::get<0>(results) = w.ToNumber(ctx_, CHECK);
std::get<1>(results) = std::get<0>(results) + Target;
StorePropImpl(base, s, std::get<1>(results), strict, e);
return std::get<Returned>(results);
}
}
template<int Target, std::size_t Returned>
JSVal IncrementProp(JSVal base,
Symbol s, bool strict, Error* e) {
base.CheckObjectCoercible(CHECK);
const JSVal w = LoadPropImpl(base, s, strict, CHECK);
if (w.IsInt32() && detail::IsIncrementOverflowSafe<Target>(w.int32())) {
std::tuple<JSVal, JSVal> results;
const int32_t target = w.int32();
std::get<0>(results) = w;
std::get<1>(results) = JSVal::Int32(target + Target);
StorePropImpl(base, s, std::get<1>(results), strict, e);
return std::get<Returned>(results);
} else {
std::tuple<double, double> results;
std::get<0>(results) = w.ToNumber(ctx_, CHECK);
std::get<1>(results) = std::get<0>(results) + Target;
StorePropImpl(base, s, std::get<1>(results), strict, e);
return std::get<Returned>(results);
}
}
JSVal LoadGlobal(JSGlobal* global,
Instruction* instr, const Symbol& s, bool strict, Error* e) {
// opcode | (dst | index) | nop | nop
if (instr[2].map == global->map()) {
// map is cached, so use previous index code
return global->Direct(instr[3].u32[0]);
} else {
// now Own Property Pattern only implemented
Slot slot;
assert(!symbol::IsArrayIndexSymbol(s));
if (global->GetOwnPropertySlot(ctx_, s, &slot)) {
if (slot.IsLoadCacheable()) {
instr[2].map = global->map();
instr[3].u32[0] = slot.offset();
return slot.value();
}
return slot.Get(ctx_, global, e);
} else {
instr[2].map = NULL;
return LoadName(ctx_->global_env(), s, strict, e);
}
}
}
bool GetPrimitiveOwnProperty(JSVal base, const Symbol& s, JSVal* res) {
// section 8.7.1 special [[Get]]
assert(base.IsPrimitive());
if (base.IsString()) {
// string short circuit
JSString* str = base.string();
if (s == symbol::length()) {
*res = JSVal::UInt32(static_cast<uint32_t>(str->size()));
return true;
}
if (symbol::IsArrayIndexSymbol(s)) {
const uint32_t index = symbol::GetIndexFromSymbol(s);
if (index < str->size()) {
*res = JSString::NewSingle(ctx_, str->At(index));
return true;
}
}
}
return false;
}
#undef CHECK
inline lv5::Context* ctx() const { return ctx_; }
private:
lv5::Context* ctx_;
};
} } } // namespace iv::lv5::railgun
#endif // IV_LV5_RAILGUN_OPERATION_H_