Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
] # props are py string pairs (prop_name, kind): kind can be either i, g or s. (init, get, set)
def __init__(self, props):
self.num = len(props)
self.props = props
def eval(self, ctx):
obj = ctx.space.NewObject()
if self.num:
obj._init(self.props, ctx.stack[-self.num:])
del ctx.stack[-self.num:]
ctx.stack.append(obj)
class LOAD_ARRAY(OP_CODE):
_params = ['num']
def __init__(self, num):
self.num = num
def eval(self, ctx):
arr = ctx.space.NewArray(self.num)
if self.num:
arr._init(ctx.stack[-self.num:])
del ctx.stack[-self.num:]
ctx.stack.append(arr)
class LOAD_THIS(OP_CODE):
def eval(self, ctx):
ctx.stack.append(ctx.THIS_BINDING)
if typ in PRIMITIVES:
prop = to_string(name)
if typ == NULL_TYPE:
raise MakeError('TypeError',
"Cannot set property '%s' of null" % prop)
elif typ == UNDEFINED_TYPE:
raise MakeError('TypeError',
"Cannot set property '%s' of undefined" % prop)
# just ignore...
else:
left.put_member(name, value)
ctx.stack.append(value)
class STORE_MEMBER_DOT(OP_CODE):
_params = ['prop']
def __init__(self, prop):
self.prop = prop
def eval(self, ctx):
value = ctx.stack.pop()
left = ctx.stack.pop()
typ = type(left)
if typ in PRIMITIVES:
if typ == NULL_TYPE:
raise MakeError('TypeError',
"Cannot set property '%s' of null" % self.prop)
elif typ == UNDEFINED_TYPE:
raise MakeError(
class POSTFIX(OP_CODE):
_params = ['cb', 'ca', 'identifier']
def __init__(self, post, incr, identifier):
self.identifier = identifier
self.cb = 1 if incr else -1
self.ca = -self.cb if post else 0
def eval(self, ctx):
target = to_number(ctx.get(self.identifier)) + self.cb
ctx.put(self.identifier, target)
ctx.stack.append(target + self.ca)
class POSTFIX_MEMBER(OP_CODE):
_params = ['cb', 'ca']
def __init__(self, post, incr):
self.cb = 1 if incr else -1
self.ca = -self.cb if post else 0
def eval(self, ctx):
name = ctx.stack.pop()
left = ctx.stack.pop()
target = to_number(get_member(left, name, ctx.space)) + self.cb
if type(left) not in PRIMITIVES:
left.put_member(name, target)
ctx.stack.append(target + self.ca)
class CALL_METHOD_DOT_NO_ARGS(OP_CODE):
_params = ['prop']
def __init__(self, prop):
self.prop = prop
def eval(self, ctx):
base = ctx.stack.pop()
func = get_member_dot(base, self.prop, ctx.space)
return bytecode_call(ctx, func, base, ())
class NOP(OP_CODE):
def eval(self, ctx):
pass
class RETURN(OP_CODE):
def eval(
self, ctx
): # remember to load the return value on stack before using RETURN op.
return (None, None)
class NEW(OP_CODE):
def eval(self, ctx):
args = ctx.stack.pop()
constructor = ctx.stack.pop()
if type(constructor) in PRIMITIVES or not hasattr(
def __init__(self, n):
self.n = n
def eval(self, ctx):
tup = tuple(ctx.stack[-self.n:])
del ctx.stack[-self.n:]
ctx.stack.append(tup)
class LOAD_UNDEFINED(OP_CODE):
def eval(self, ctx):
ctx.stack.append(undefined)
class LOAD_NULL(OP_CODE):
def eval(self, ctx):
ctx.stack.append(null)
class LOAD_BOOLEAN(OP_CODE):
_params = ['val']
def __init__(self, val):
assert val in (0, 1)
self.val = bool(val)
def eval(self, ctx):
ctx.stack.append(self.val)
class LOAD_STRING(OP_CODE):
class CALL_METHOD_DOT(OP_CODE):
_params = ['prop']
def __init__(self, prop):
self.prop = prop
def eval(self, ctx):
args = ctx.stack.pop()
base = ctx.stack.pop()
func = get_member_dot(base, self.prop, ctx.space)
return bytecode_call(ctx, func, base, args)
class CALL_NO_ARGS(OP_CODE):
def eval(self, ctx):
func = ctx.stack.pop()
return bytecode_call(ctx, func, ctx.space.GlobalObj, ())
class CALL_METHOD_NO_ARGS(OP_CODE):
def eval(self, ctx):
prop = ctx.stack.pop()
base = ctx.stack.pop()
func = get_member(base, prop, ctx.space)
return bytecode_call(ctx, func, base, ())
# --------------- CALLS --------------
def bytecode_call(ctx, func, this, args):
if type(func) is not PyJsFunction:
raise MakeError('TypeError', "%s is not a function" % Type(func))
if func.is_native: # call to built-in function or method
ctx.stack.append(func.call(this, args))
return None
# therefore not native. we have to return (new_context, function_label) to instruct interpreter to call
return func._generate_my_context(this, args), func.code
class CALL(OP_CODE):
def eval(self, ctx):
args = ctx.stack.pop()
func = ctx.stack.pop()
return bytecode_call(ctx, func, ctx.space.GlobalObj, args)
class CALL_METHOD(OP_CODE):
def eval(self, ctx):
args = ctx.stack.pop()
prop = ctx.stack.pop()
base = ctx.stack.pop()
func = get_member(base, prop, ctx.space)
return bytecode_call(ctx, func, base, args)
return bytecode_call(ctx, func, ctx.space.GlobalObj, args)
class CALL_METHOD(OP_CODE):
def eval(self, ctx):
args = ctx.stack.pop()
prop = ctx.stack.pop()
base = ctx.stack.pop()
func = get_member(base, prop, ctx.space)
return bytecode_call(ctx, func, base, args)
class CALL_METHOD_DOT(OP_CODE):
_params = ['prop']
def __init__(self, prop):
self.prop = prop
def eval(self, ctx):
args = ctx.stack.pop()
base = ctx.stack.pop()
func = get_member_dot(base, self.prop, ctx.space)
return bytecode_call(ctx, func, base, args)
class CALL_NO_ARGS(OP_CODE):
def eval(self, ctx):
def __init__(self, identifier):
self.identifier = identifier
# 11.1.2
def eval(self, ctx):
ctx.stack.append(ctx.get(self.identifier, throw=True))
class LOAD_MEMBER(OP_CODE):
def eval(self, ctx):
prop = ctx.stack.pop()
obj = ctx.stack.pop()
ctx.stack.append(get_member(obj, prop, ctx.space))
class LOAD_MEMBER_DOT(OP_CODE):
_params = ['prop']
def __init__(self, prop):
self.prop = prop
def eval(self, ctx):
obj = ctx.stack.pop()
ctx.stack.append(get_member_dot(obj, self.prop, ctx.space))
# --------------- STORING --------------
class STORE(OP_CODE):
_params = ['identifier']
func = ctx.stack.pop()
return bytecode_call(ctx, func, ctx.space.GlobalObj, ())
class CALL_METHOD_NO_ARGS(OP_CODE):
def eval(self, ctx):
prop = ctx.stack.pop()
base = ctx.stack.pop()
func = get_member(base, prop, ctx.space)
return bytecode_call(ctx, func, base, ())
class CALL_METHOD_DOT_NO_ARGS(OP_CODE):
_params = ['prop']
def __init__(self, prop):
self.prop = prop
def eval(self, ctx):
base = ctx.stack.pop()
func = get_member_dot(base, self.prop, ctx.space)
return bytecode_call(ctx, func, base, ())
class NOP(OP_CODE):
def eval(self, ctx):
pass