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 <[email protected]> |
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\Modules; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Specs\AnonymousObjectSpec; |
20
|
|
|
use Pinepain\JsSandbox\Specs\Builder\FunctionSpecBuilder; |
21
|
|
|
use Pinepain\JsSandbox\Specs\Builder\ObjectSpecBuilderInterface; |
22
|
|
|
use Pinepain\JsSandbox\Specs\ObjectSpec; |
23
|
|
|
use Pinepain\JsSandbox\Specs\ObjectSpecsCollectionInterface; |
24
|
|
|
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunction; |
25
|
|
|
use Pinepain\JsSandbox\Wrappers\Runtime\RuntimeFunctionInterface; |
26
|
|
|
use Pinepain\JsSandbox\Wrappers\WrapperInterface; |
27
|
|
|
use V8\Context; |
28
|
|
|
use V8\FunctionObject; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class ModulesService |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var ObjectSpecBuilderInterface |
35
|
|
|
*/ |
36
|
|
|
private $object; |
37
|
|
|
/** |
38
|
|
|
* @var FunctionSpecBuilder |
39
|
|
|
*/ |
40
|
|
|
private $function; |
41
|
|
|
|
42
|
|
|
public function __construct(ObjectSpecBuilderInterface $object, FunctionSpecBuilder $function) |
43
|
|
|
{ |
44
|
|
|
$this->object = $object; |
45
|
|
|
$this->function = $function; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function registerObjectSpecs(ObjectSpecsCollectionInterface $specs) |
49
|
|
|
{ |
50
|
|
|
$module_object_spec = new ObjectSpec('Module', $this->object->build([ |
51
|
|
|
'id' => 'get: getId()', |
52
|
|
|
'filename' => 'get: getFilename()', |
53
|
|
|
// 'dirname' => 'get: getDirname()', // NOTE: this is non-standard extension compared to node modules system |
|
|
|
|
54
|
|
|
'loaded' => 'get: isLoaded()', |
55
|
|
|
'exports' => 'get: getExports() set: setExports(raw)', |
56
|
|
|
'parent' => 'get: getParent()', |
57
|
|
|
'children' => 'get: getChildren()', |
58
|
|
|
])); |
59
|
|
|
|
60
|
|
|
$specs->put(Module::class, $module_object_spec); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function createNativeFunctionWrapper(Context $context, RequireCallbackInterface $require_object, WrapperInterface $wrapper): NativeRequireFunctionWrapperInterface |
64
|
|
|
{ |
65
|
|
|
$runtime_function = $this->createRuntimeFunction($require_object); |
66
|
|
|
$function_object = $this->createFunctionObject($context, $runtime_function, $wrapper); |
67
|
|
|
|
68
|
|
|
return new NativeRequireFunctionWrapper($context->getIsolate(), $context, $function_object, $wrapper, $context->globalObject()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Context $context |
73
|
|
|
* @param RuntimeFunctionInterface $runtime_function |
74
|
|
|
* @param WrapperInterface $wrapper |
75
|
|
|
* |
76
|
|
|
* @return FunctionObject |
77
|
|
|
*/ |
78
|
|
|
protected function createFunctionObject(Context $context, RuntimeFunctionInterface $runtime_function, WrapperInterface $wrapper): FunctionObject |
79
|
|
|
{ |
80
|
|
|
/** @var FunctionObject $res */ |
81
|
|
|
$res = $wrapper->wrap($context->getIsolate(), $context, $runtime_function); |
82
|
|
|
|
83
|
|
|
return $res; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function createRuntimeFunction(RequireCallbackInterface $require_object): RuntimeFunctionInterface |
87
|
|
|
{ |
88
|
|
|
$require_spec = $this->function->build('@inject-context (id: string)'); |
89
|
|
|
|
90
|
|
|
$require_function_object_spec = new AnonymousObjectSpec($this->object->build([ |
91
|
|
|
'main' => 'get: getMain()', |
92
|
|
|
'resolve' => '(id: string)', |
93
|
|
|
])); |
94
|
|
|
|
95
|
|
|
return new RuntimeFunction('require', [$require_object, 'callback'], $require_spec, $require_object, $require_function_object_spec); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.