Example #1
0
 def test_call_args(self):
     space = ObjSpace()
     interp = MockInterpreter(space)
     sin = interp.locate_function("sin")
     w_res = space.call_args(sin, [space.wrap(1.2)])
     assert space.float_w(w_res) == math.sin(1.2)
     max = interp.locate_function("max")
     w_res = space.call_args(max, [space.wrap(2), space.wrap(15),
                                   space.wrap(3)])
     assert space.int_w(w_res) == 15
     w_res = space.call_args(max, [W_Reference(space.wrap(2)),
                                   space.wrap(15),
                                   space.wrap(3)])
     assert space.int_w(w_res) == 15
     str_repeat = interp.locate_function("str_repeat")
     w_res = space.call_args(str_repeat, [space.newstr("a"), space.wrap(3)])
     assert space.str_w(w_res) == "aaa"
     source = """<?php
     function f($a, $b) {
         return $a + 10 * $b;
     }
     """
     bc = compile_php('<input>', source, space, interp)
     interp.run_main(space, bc)
     f = interp.locate_function("f")
     w_res = space.call_args(f, [space.wrap(1), space.wrap(2)])
     assert space.int_w(w_res) == 21
Example #2
0
 def test_call_args(self):
     space = ObjSpace()
     interp = MockInterpreter(space)
     sin = interp.locate_function("sin")
     w_res = space.call_args(sin, [space.wrap(1.2)])
     assert space.float_w(w_res) == math.sin(1.2)
     max = interp.locate_function("max")
     w_res = space.call_args(
         max, [space.wrap(2), space.wrap(15),
               space.wrap(3)])
     assert space.int_w(w_res) == 15
     w_res = space.call_args(
         max, [W_Reference(space.wrap(2)),
               space.wrap(15),
               space.wrap(3)])
     assert space.int_w(w_res) == 15
     str_repeat = interp.locate_function("str_repeat")
     w_res = space.call_args(str_repeat, [space.newstr("a"), space.wrap(3)])
     assert space.str_w(w_res) == "aaa"
     source = """<?php
     function f($a, $b) {
         return $a + 10 * $b;
     }
     """
     bc = compile_php('<input>', source, space, interp)
     interp.run_main(space, bc)
     f = interp.locate_function("f")
     w_res = space.call_args(f, [space.wrap(1), space.wrap(2)])
     assert space.int_w(w_res) == 21
Example #3
0
 def test_get_printable_location(self):
     source = "<? $a = 3; ?>"
     space = getspace()
     bc = compile_php('<input>', source, space)
     assert get_printable_location(0, bc) == "<main> 1 VAR_PTR"
     # it may be called with pc = len(bc.code) during jitting
     assert get_printable_location(len(bc.code), bc) == "<main> END ?"
Example #4
0
 def test_serialize_with_calls(self):
     source = """<?
     function f($a) {
         return $a + 4;
     }
     echo f(3);
     ?>"""
     space = getspace()
     bc = compile_php('<input>', source, space)
     dump = bc.serialize(space)
     bc2 = unserialize(dump, space)
     interp = MockInterpreter(space)
     interp.run_main(space, bc2)
     assert space.int_w(interp.output[0]) == 3 + 4
Example #5
0
def _assert(space, w_check, desc=None):
    """ Checks if assertion is FALSE"""
    if w_check.tp == space.tp_str:
        source_orig = space.str_w(w_check)
        source = "<? return %s ?>" % source_orig
        bc = compile_php(None, source, space)
        interp = space.ec.interpreter
        w_res = interp.run_local_include(bc, interp.global_frame)
        if not w_res.is_true(space):
            space.ec.warn('assert(): Assertion "%s" failed' % source_orig)
            return space.w_Null
    if not w_check.is_true(space):
        space.ec.warn('assert(): Assertion failed')
        return space.w_Null
    return space.wrap(w_check.is_true(space))
Example #6
0
def run(args):
    assert len(args) == 1, "XXX only supports one argument, a php file"
    filename = args[0]
    f = open(filename, 'r')
    source = f.read()
    f.close()
    space = getspace()
    bc = compile_php(filename, source, space)
    print '-=- %s -=-' % (filename,)
    interp = Interpreter(space)
    interp.setup(False, args)
    try:
        interp.run_main(space, bc)
    except ExplicitExitException, e:
        sys.exit(e.code)
Example #7
0
def _assert(space, w_check, desc=None):
    """ Checks if assertion is FALSE"""
    if w_check.tp == space.tp_str:
        source_orig = space.str_w(w_check)
        source = "<? return %s ?>" % source_orig
        bc = compile_php(None, source, space)
        interp = space.ec.interpreter
        w_res = interp.run_local_include(bc, interp.global_frame)
        if not w_res.is_true(space):
            space.ec.warn('assert(): Assertion "%s" failed' % source_orig)
            return space.w_Null
    if not w_check.is_true(space):
            space.ec.warn('assert(): Assertion failed')
            return space.w_Null
    return space.wrap(w_check.is_true(space))
Example #8
0
def run(args):
    assert len(args) == 1, "XXX only supports one argument, a php file"
    filename = args[0]
    f = open(filename, 'r')
    source = f.read()
    f.close()
    space = getspace()
    bc = compile_php(filename, source, space)
    print '-=- %s -=-' % (filename, )
    interp = Interpreter(space)
    interp.setup(False, args)
    try:
        interp.run_main(space, bc)
    except ExplicitExitException, e:
        sys.exit(e.code)
Example #9
0
 def test_serialize_array_constants(self):
     source = """<?
     $a = array("a", "b");
     $b = array("a"=>"b");
     echo $a[1];
     echo $b["a"];
     ?>"""
     space = getspace()
     bc = compile_php('<input>', source, space)
     dump = bc.serialize(space)
     bc2 = unserialize(dump, space)
     interp = MockInterpreter(space)
     interp.run_main(space, bc2)
     assert space.str_w(interp.output[0]) == "b"
     assert space.str_w(interp.output[1]) == "b"
Example #10
0
 def compile_file(self, fname, space):
     absname = abspath(fname)
     now = time.time()
     try:
         bc, tstamp = self.cached_files[absname]
         if now - tstamp >= self.timeout:
             mtime = os.stat(absname).st_mtime
             if mtime > tstamp:
                 raise KeyError
     except KeyError:
         f = open(absname)
         data = f.read(-1)
         f.close()
         tstamp = os.stat(absname).st_mtime
         bc = compile_php(absname, data, space)
         self.cached_files[absname] = (bc, tstamp)
     return bc
Example #11
0
 def test_serialize_with_classes(self):
     source = """<?
     class X {
         function __construct() {
            $this->x = 3;
         }
     }
     $x = new X();
     echo $x->x;
     ?>"""
     space = getspace()
     bc = compile_php('<input>', source, space)
     dump = bc.serialize(space)
     bc2 = unserialize(dump, space)
     interp = MockInterpreter(space)
     interp.run_main(space, bc2)
     assert space.int_w(interp.output[0]) == 3
Example #12
0
 def test_basic_serialize(self):
     source = "<? $a = 3; var_dump($a);?>"
     space = getspace()
     bc = compile_php('<input>', source, space)
     dump = bc.serialize(space)
     bc2 = unserialize(dump, space)
     assert bc.dump() == bc2.dump()
     assert space.int_w(bc2.consts[0]) == 3
     assert bc2.name == bc.name
     assert bc2.filename == bc.filename
     assert bc2.startlineno == bc.startlineno
     assert bc2.sourcelines == bc.sourcelines
     assert bc.names == bc2.names
     assert bc.varnames == bc2.varnames
     interp = MockInterpreter(space)
     interp.run_main(space, bc2)
     assert interp.output[0] == 'int(3)\n'
Example #13
0
 def compile_file(self, fname, space):
     absname = abspath(fname)
     now = time.time()
     try:
         bc, tstamp = self.cached_files[absname]
         if now - tstamp >= self.timeout:
             mtime = os.stat(absname).st_mtime
             if mtime > tstamp:
                 raise KeyError
     except KeyError:
         f = open(absname)
         data = f.read(-1)
         f.close()
         tstamp = os.stat(absname).st_mtime
         bc = compile_php(absname, data, space)
         self.cached_files[absname] = (bc, tstamp)
     return bc
Example #14
0
    def _really_compile(self, space, abs_fname):

        if abs_fname is None:
            f = os.fdopen(0)  # open stdin
            abs_fname = '<stdin>'  # PHP uses '-' in that case
        else:
            f = open(abs_fname)

        data = f.read(-1)

        bc = compile_php(abs_fname, data, space)

        if abs_fname != '<stdin>':  # if not stdin, cache bytecode
            f.close()  # also don't close if stdin
            tstamp = os.stat(abs_fname).st_mtime
            self.cached_files[abs_fname] = (bc, tstamp)

        return bc
Example #15
0
    def _really_compile(self, space, abs_fname):

        if abs_fname is None:
            f = os.fdopen(0)  # open stdin
            abs_fname = '<stdin>'  # PHP uses '-' in that case
        else:
            f = open(abs_fname)

        data = f.read(-1)

        bc = compile_php(abs_fname, data, space)

        if abs_fname != '<stdin>':  # if not stdin, cache bytecode
            f.close()  # also don't close if stdin
            tstamp = os.stat(abs_fname).st_mtime
            self.cached_files[abs_fname] = (bc, tstamp)

        return bc
def test_line_start_offset():
    space = ObjSpace()
    MockInterpreter(space)
    bc = compile_php('<input>', 'Hi there\n', space)
    assert bc.startlineno == 1
Example #17
0
def read_code():
    source = open(filename).read()
    space = getspace()
    bc = compile_php('<input>', source, space)
    return llstr(bc.serialize(space))
Example #18
0
 def compile(self, source):
     return compile_php('<input>', source, self.space, self)
Example #19
0
def test_line_start_offset():
    space = ObjSpace()
    MockInterpreter(space)
    bc = compile_php('<input>', 'Hi there\n', space)
    assert bc.startlineno == 1
Example #20
0
def read_code():
    source = open(filename).read()
    space = getspace()
    bc = compile_php('<input>', source, space)
    return llstr(bc.serialize(space))
Example #21
0
 def compile(self, source):
     return compile_php('<input>', source, self.space, self)