Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Binding interface #873

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/objects/test_bindingobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,41 @@ def test_unused_closure(self, space):
return binding.eval('12')
""")
assert space.int_w(w_res) == 12

def test_local_variable_definedp(self, space):
w_res = space.execute("""
a = 5
return binding.local_variable_defined?('a')
""")
assert w_res == space.w_true

def test_local_variable_get(self, space):
w_res = space.execute("""
a = 5
return binding.local_variable_get('a')
""")
assert space.int_w(w_res) == 5

def test_local_variable_set(self, space):
w_res = space.execute("""
a = 5
binding.local_variable_set('a', 42)
return a
""")
assert space.int_w(w_res) == 42

def test_local_variables(self, space):
w_res = space.execute("""
a = 5
x = 'foo'
return binding.local_variables
""")
res_w = space.listview(w_res)
assert space.str_w(res_w[0]) == 'a'
assert space.str_w(res_w[1]) == 'x'

def test_receiver(self, space):
w_res = space.execute("""
return binding.receiver == self
""")
assert w_res == space.w_true
38 changes: 36 additions & 2 deletions topaz/objects/bindingobject.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from rpython.rlib import jit

from topaz.astcompiler import SymbolTable
from topaz.closure import ClosureCell
from topaz.module import ClassDef
from topaz.objects.objectobject import W_Object


class W_BindingObject(W_Object):
classdef = ClassDef("Binding", W_Object.classdef)
_immutable_fields_ = ["names[*]", "cells[*]", "w_self", "lexical_scope"]
_immutable_fields_ = ["names[*]?", "cells[*]?", "w_self", "lexical_scope"]

def __init__(self, space, names, cells, w_self, lexical_scope):
W_Object.__init__(self, space)
Expand All @@ -22,8 +25,39 @@ def method_eval(self, space, source):
for name in self.names:
symtable.cells[name] = symtable.FREEVAR
bc = space.compile(source, "(eval)", symtable=symtable)
frame = space.create_frame(bc, w_self=self.w_self, lexical_scope=self.lexical_scope)
frame = space.create_frame(
bc, w_self=self.w_self, lexical_scope=self.lexical_scope)
for idx, cell in enumerate(self.cells):
frame.cells[idx + len(bc.cellvars)] = cell
with space.getexecutioncontext().visit_frame(frame):
return space.execute_frame(frame, bc)

@classdef.method("local_variable_defined?", key="symbol")
def method_local_variable_definedp(self, space, key):
return space.newbool(key in self.names)

@jit.unroll_safe
@classdef.method("local_variable_get", key="symbol")
def method_local_variable_get(self, space, key):
for idx, name in enumerate(self.names):
if name == key:
return self.cells[idx].get(space, None, 0)
return space.w_nil

@jit.unroll_safe
@classdef.method("local_variable_set", key="symbol")
def method_local_variable_set(self, space, key, w_value):
for idx, name in enumerate(self.names):
if name == key:
self.cells[idx].set(space, None, 0, w_value)
return
self.names.append(key)
self.cells.append(ClosureCell(w_value))

@classdef.method("local_variables")
def method_local_variables(self, space):
return space.newarray([space.newstr_fromstr(n) for n in self.names])

@classdef.method("receiver")
def method_receiver(self, space):
return self.w_self