def test_multiple_contexts(self): with v8.Context(self.isolate) as c1: script = v8.Script( c1, '<test>', ''' function f() { return x; } f(); ''', ) with self.assertRaisesRegex( v8.JavaScriptError, r'ReferenceError: x is not defined', ): script.run(c1) c1['x'] = 'Hello, World!' self.assertEqual(script.run(c1), 'Hello, World!') # Run with another context. It does not seem to matter. Then # why does v8::Script::Run takes a v8::Context for argument? with v8.Context(self.isolate) as c2: self.assertEqual(script.run(c2), 'Hello, World!')
def test_syntax_error(self): context = self.stack.enter_context(v8.Context(self.isolate)) with self.assertRaisesRegex( v8.JavaScriptError, r'SyntaxError: Invalid or unexpected token', ): v8.Script(context, '<test>', '#')
def test_context(self): with v8.Context(self.isolate) as context: self.assertEqual(len(context), 0) v8.run(context, 'x = 1; y = \'Hello, World!\'; z = true;') self.assertEqual(len(context), 3) self.assertEqual(list(context), ['x', 'y', 'z']) for name, expect in [ ('x', 1), ('y', 'Hello, World!'), ('z', True), ]: with self.subTest(name): self.assertIn(name, context) self.assertEqual(context[name], expect) context['p'] = 0 context['x'] = None self.assertEqual(len(context), 4) self.assertIn('p', context) self.assertEqual(context['p'], 0) self.assertIsNone(context['x']) # What does re-entering v8::Context actually do? with context: v8.run(context, 'a = 3.5;') self.assertEqual(len(context), 5) self.assertEqual(len(context), 5) self.assertEqual(len(context), 5)
def test_context(self): with contextlib.ExitStack() as stack: isolate = stack.enter_context(v8.Isolate()) stack.enter_context(isolate.scope()) with v8.HandleScope(isolate): with v8.Context(isolate) as c1: c1['x'] = 'foo bar' global_context = stack.enter_context( v8.GlobalContext(isolate, c1)) self.assertEqual( global_context.get(isolate)['x'], 'foo bar', ) self.assertEqual( global_context.get(isolate)['x'], 'foo bar', ) # In another handle scope. with v8.HandleScope(isolate): self.assertEqual( global_context.get(isolate)['x'], 'foo bar', )
def setUp(self): super().setUp() self.assertEqual(v8.Isolate.num_alive, 0) self.stack = contextlib.ExitStack() self.stack.__enter__() self.isolate = self.stack.enter_context(v8.Isolate()) self.assertEqual(v8.Isolate.num_alive, 1) self.stack.enter_context(self.isolate.scope()) self.stack.enter_context(v8.HandleScope(self.isolate)) self.context = self.stack.enter_context(v8.Context(self.isolate))
def test_no_re_enter(self): self.assertEqual(v8.Isolate.num_alive, 0) with v8.Isolate() as i1, i1.scope(), v8.HandleScope(i1): c1 = v8.Context(i1) with v8.GlobalContext(i1, c1) as global_context: with self.assertRaisesRegex( RuntimeError, r'this context manager only allows being entered once', ): global_context.__enter__() with self.assertRaisesRegex( RuntimeError, r'this context manager only allows being entered once', ): global_context.__enter__() self.assertEqual(v8.Isolate.num_alive, 0)
def test_throw(self): context = self.stack.enter_context(v8.Context(self.isolate)) script = v8.Script(context, '<test>', 'throw "Hello, World!";') with self.assertRaisesRegex(v8.JavaScriptError, r'"Hello, World!"'): script.run(context)
def test_run(self): context = self.stack.enter_context(v8.Context(self.isolate)) script = v8.Script(context, '<test>', '"Hello, World!";') self.assertEqual(script.run(context), 'Hello, World!')