Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 294bd66

Browse files
committed
Fix invalid native function wrapping from exec context
1 parent acfd8f5 commit 294bd66

File tree

3 files changed

+186
-12
lines changed

3 files changed

+186
-12
lines changed

src/Wrappers/FunctionComponents/Runtime/ExecutionContext.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Pinepain\JsSandbox\NativeWrappers\NativeFunctionWrapper;
2020
use Pinepain\JsSandbox\NativeWrappers\NativeFunctionWrapperInterface;
2121
use Pinepain\JsSandbox\Specs\FunctionSpecInterface;
22-
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunction;
22+
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunctionInterface;
2323
use Pinepain\JsSandbox\Wrappers\WrapperInterface;
2424
use V8\Context;
2525
use V8\FunctionCallbackInfo;
@@ -35,7 +35,7 @@ class ExecutionContext implements ExecutionContextInterface
3535
*/
3636
private $wrapper;
3737
/**
38-
* @var RuntimeFunction
38+
* @var RuntimeFunctionInterface
3939
*/
4040
private $runtime_function;
4141
/**
@@ -47,7 +47,7 @@ class ExecutionContext implements ExecutionContextInterface
4747
*/
4848
private $spec;
4949

50-
public function __construct(WrapperInterface $wrapper, RuntimeFunction $runtime_function, FunctionCallbackInfo $args, FunctionSpecInterface $spec)
50+
public function __construct(WrapperInterface $wrapper, RuntimeFunctionInterface $runtime_function, FunctionCallbackInfo $args, FunctionSpecInterface $spec)
5151
{
5252
$this->wrapper = $wrapper;
5353
$this->runtime_function = $runtime_function;
@@ -75,6 +75,11 @@ public function getWrapper(): WrapperInterface
7575
return $this->wrapper;
7676
}
7777

78+
public function getRuntimeFunction(): RuntimeFunctionInterface
79+
{
80+
return $this->runtime_function;
81+
}
82+
7883
public function getFunctionCallbackInfo(): FunctionCallbackInfo
7984
{
8085
return $this->args;
@@ -85,11 +90,6 @@ public function getFunctionSpec(): FunctionSpecInterface
8590
return $this->spec;
8691
}
8792

88-
public function getRuntimeFunction(): RuntimeFunction
89-
{
90-
return $this->runtime_function;
91-
}
92-
9393
public function getFunctionObject(): FunctionObject
9494
{
9595
// At this time we should always have request RuntimeFunction be in cache
@@ -103,6 +103,6 @@ public function wrap($value)
103103

104104
public function wrapNativeFunction(ObjectValue $recv, FunctionObject $function_object): NativeFunctionWrapperInterface
105105
{
106-
return new NativeFunctionWrapper($this->getContext(), $recv, $function_object, $this->getWrapper());
106+
return new NativeFunctionWrapper($this->getIsolate(), $this->getContext(), $function_object, $this->getWrapper(), $recv);
107107
}
108108
}

src/Wrappers/FunctionComponents/Runtime/ExecutionContextInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use Pinepain\JsSandbox\NativeWrappers\NativeFunctionWrapperInterface;
2020
use Pinepain\JsSandbox\Specs\FunctionSpecInterface;
21-
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunction;
21+
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunctionInterface;
2222
use Pinepain\JsSandbox\Wrappers\WrapperInterface;
2323
use V8\Context;
2424
use V8\FunctionCallbackInfo;
@@ -37,12 +37,12 @@ public function getThis(): ObjectValue;
3737

3838
public function getWrapper(): WrapperInterface;
3939

40+
public function getRuntimeFunction(): RuntimeFunctionInterface;
41+
4042
public function getFunctionCallbackInfo(): FunctionCallbackInfo;
4143

4244
public function getFunctionSpec(): FunctionSpecInterface;
4345

44-
public function getRuntimeFunction(): RuntimeFunction;
45-
4646
public function getFunctionObject(): FunctionObject;
4747

4848
public function wrap($value);
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* This file is part of the pinepain/js-sandbox PHP library.
5+
*
6+
* Copyright (c) 2016-2017 Bogdan Padalko <pinepain@gmail.com>
7+
*
8+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
9+
*
10+
* For the full copyright and license information, please view the
11+
* LICENSE file that was distributed with this source or visit
12+
* http://opensource.org/licenses/MIT
13+
*/
14+
15+
16+
namespace Pinepain\JsSandbox\Tests\Wrappers\FunctionComponents\Runtime;
17+
18+
19+
use PHPUnit\Framework\TestCase;
20+
use PHPUnit_Framework_MockObject_MockObject;
21+
use Pinepain\JsSandbox\NativeWrappers\NativeFunctionWrapperInterface;
22+
use Pinepain\JsSandbox\Specs\FunctionSpecInterface;
23+
use Pinepain\JsSandbox\Wrappers\FunctionComponents\Runtime\ExecutionContext;
24+
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunctionInterface;
25+
use Pinepain\JsSandbox\Wrappers\WrapperInterface;
26+
use V8\Context;
27+
use V8\FunctionCallbackInfo;
28+
use V8\FunctionObject;
29+
use V8\Isolate;
30+
use V8\ObjectValue;
31+
32+
33+
class ExecutionContextTest extends TestCase
34+
{
35+
/**
36+
* @var WrapperInterface|PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $wrapper;
39+
/**
40+
* @var RuntimeFunctionInterface|PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $runtime_function;
43+
/**
44+
* @var FunctionCallbackInfo|PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $args;
47+
/**
48+
* @var FunctionSpecInterface|PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
private $spec;
51+
/**
52+
* @var Isolate
53+
*/
54+
private $isolate;
55+
/**
56+
* @var Context
57+
*/
58+
private $context;
59+
60+
public function setUp()
61+
{
62+
$this->wrapper = $this->getMockBuilder(WrapperInterface::class)->getMockForAbstractClass();
63+
$this->runtime_function = $this->getMockBuilder(RuntimeFunctionInterface::class)->getMockForAbstractClass();
64+
$this->args = $this->getMockBuilder(FunctionCallbackInfo::class)
65+
->setMethods(['getIsolate', 'getContext'])
66+
->getMockForAbstractClass();
67+
68+
$this->isolate = new Isolate();
69+
$this->context = new Context($this->isolate);
70+
71+
$this->args->expects($this->any())
72+
->method('getIsolate')
73+
->willReturn($this->isolate);
74+
75+
$this->args->expects($this->any())
76+
->method('getContext')
77+
->willReturn($this->context);
78+
79+
$this->spec = $this->getMockBuilder(FunctionSpecInterface::class)->getMockForAbstractClass();
80+
}
81+
82+
public function testDelegatedGetters()
83+
{
84+
$isolate = new Isolate();
85+
$context = new Context($isolate);
86+
$this_obj = new ObjectValue($context);
87+
88+
$this->args = $this->getMockBuilder(FunctionCallbackInfo::class)
89+
->setMethods(['getIsolate', 'getContext', 'this'])
90+
->getMockForAbstractClass();
91+
92+
$this->args->expects($this->any())
93+
->method('getIsolate')
94+
->willReturn($isolate);
95+
96+
$this->args->expects($this->any())
97+
->method('getContext')
98+
->willReturn($context);
99+
100+
$this->args->expects($this->any())
101+
->method('this')
102+
->willReturn($this_obj);
103+
104+
$exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec);
105+
106+
$this->assertSame($isolate, $exec->getIsolate());
107+
$this->assertSame($context, $exec->getContext());
108+
$this->assertSame($this_obj, $exec->getThis());
109+
}
110+
111+
public function testGetters()
112+
{
113+
$exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec);
114+
115+
$this->assertSame($this->wrapper, $exec->getWrapper());
116+
$this->assertSame($this->runtime_function, $exec->getRuntimeFunction());
117+
$this->assertSame($this->args, $exec->getFunctionCallbackInfo());
118+
$this->assertSame($this->spec, $exec->getFunctionSpec());
119+
}
120+
121+
public function testWrapping()
122+
{
123+
$value = 'test string';
124+
$wrapped = $this->getMockBuilder(FunctionObject::class)
125+
->disableOriginalConstructor()
126+
->getMock();
127+
128+
$this->wrapper = $this->getMockBuilder(WrapperInterface::class)
129+
->setMethods(['wrap'])
130+
->getMockForAbstractClass();
131+
132+
$this->wrapper->expects($this->once())
133+
->method('wrap')
134+
->with($this->isolate, $this->context, $value)
135+
->willReturn($wrapped);
136+
137+
$exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec);
138+
139+
$this->assertSame($wrapped, $exec->wrap($value));
140+
141+
142+
$this->wrapper = $this->getMockBuilder(WrapperInterface::class)
143+
->setMethods(['wrap'])
144+
->getMockForAbstractClass();
145+
146+
$this->wrapper->expects($this->once())
147+
->method('wrap')
148+
->with($this->isolate, $this->context, $this->runtime_function)
149+
->willReturn($wrapped);
150+
151+
$exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec);
152+
153+
$this->assertSame($wrapped, $exec->getFunctionObject());
154+
}
155+
156+
public function testWrapNativeFunction()
157+
{
158+
/** @var ObjectValue|PHPUnit_Framework_MockObject_MockObject $recv */
159+
$recv = $this->getMockBuilder(ObjectValue::class)
160+
->disableOriginalConstructor()
161+
->getMock();
162+
163+
/** @var FunctionObject|PHPUnit_Framework_MockObject_MockObject $target */
164+
$target = $this->getMockBuilder(FunctionObject::class)
165+
->disableOriginalConstructor()
166+
->getMock();
167+
168+
$exec = new ExecutionContext($this->wrapper, $this->runtime_function, $this->args, $this->spec);
169+
170+
$wrapper = $exec->wrapNativeFunction($recv, $target);
171+
172+
$this->assertInstanceOf(NativeFunctionWrapperInterface::class, $wrapper);
173+
}
174+
}

0 commit comments

Comments
 (0)