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

Commit 500c855

Browse files
committed
Add V8\ScriptCompiler\Source class
1 parent 39fb2bb commit 500c855

File tree

6 files changed

+485
-0
lines changed

6 files changed

+485
-0
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ if test "$PHP_V8" != "no"; then
170170
src/php_v8_unbound_script.cc \
171171
src/php_v8_cached_data.cc \
172172
src/php_v8_compile_options.cc \
173+
src/php_v8_source.cc \
173174
src/php_v8_data.cc \
174175
src/php_v8_value.cc \
175176
src/php_v8_primitive.cc \

src/php_v8_source.cc

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* This file is part of the pinepain/php-v8 PHP extension.
3+
*
4+
* Copyright (c) 2015-2017 Bogdan Padalko <pinepain@gmail.com>
5+
*
6+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
7+
*
8+
* For the full copyright and license information, please view the
9+
* LICENSE file that was distributed with this source or visit
10+
* http://opensource.org/licenses/MIT
11+
*/
12+
13+
#ifdef HAVE_CONFIG_H
14+
#include "config.h"
15+
#endif
16+
17+
#include "php_v8_source.h"
18+
#include "php_v8_cached_data.h"
19+
#include "php_v8_string.h"
20+
#include "php_v8.h"
21+
22+
zend_class_entry* php_v8_source_class_entry;
23+
#define this_ce php_v8_source_class_entry
24+
25+
void php_v8_update_source_cached_data(zval *src_zv, v8::ScriptCompiler::Source *source) {
26+
if (!source->GetCachedData()) {
27+
return;
28+
}
29+
30+
zval tmp;
31+
zval rv;
32+
33+
zval *cached_data_zv = zend_read_property(this_ce, src_zv, ZEND_STRL("cached_data"), 0, &rv);
34+
35+
if (!ZVAL_IS_NULL(cached_data_zv)) {
36+
return;
37+
}
38+
39+
php_v8_create_cached_data(&tmp, source->GetCachedData());
40+
41+
zend_update_property(this_ce, src_zv, ZEND_STRL("cached_data"), &tmp);
42+
}
43+
44+
static PHP_METHOD(V8Source, __construct)
45+
{
46+
zval *source_string_zv = NULL;
47+
zval *origin_zv = NULL;
48+
zval *cached_data_zv = NULL;
49+
50+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|o!o!", &source_string_zv, &origin_zv, &cached_data_zv) == FAILURE) {
51+
return;
52+
}
53+
54+
PHP_V8_VALUE_FETCH_WITH_CHECK(source_string_zv, php_v8_string);
55+
56+
zend_update_property(this_ce, getThis(), ZEND_STRL("source_string"), source_string_zv);
57+
58+
if (origin_zv != NULL) {
59+
zend_update_property(this_ce, getThis(), ZEND_STRL("origin"), origin_zv);
60+
}
61+
62+
if (cached_data_zv != NULL) {
63+
zend_update_property(this_ce, getThis(), ZEND_STRL("cached_data"), cached_data_zv);
64+
}
65+
}
66+
67+
static PHP_METHOD(V8Source, GetSourceString)
68+
{
69+
zval rv;
70+
71+
if (zend_parse_parameters_none() == FAILURE) {
72+
return;
73+
}
74+
75+
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("source_string"), 0, &rv), 1, 0);
76+
}
77+
78+
static PHP_METHOD(V8Source, GetScriptOrigin)
79+
{
80+
zval rv;
81+
82+
if (zend_parse_parameters_none() == FAILURE) {
83+
return;
84+
}
85+
86+
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("origin"), 0, &rv), 1, 0);
87+
}
88+
89+
static PHP_METHOD(V8Source, GetCachedData)
90+
{
91+
zval rv;
92+
93+
if (zend_parse_parameters_none() == FAILURE) {
94+
return;
95+
}
96+
97+
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("cached_data"), 0, &rv), 1, 0);
98+
}
99+
100+
101+
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_source___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
102+
ZEND_ARG_OBJ_INFO(0, source_string, V8\\StringValue, 0)
103+
ZEND_ARG_OBJ_INFO(0, origin, V8\\ScriptOrigin, 1)
104+
ZEND_ARG_OBJ_INFO(0, cached_data, V8\\ScriptCompiler\\CachedData, 1)
105+
ZEND_END_ARG_INFO()
106+
107+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_source_GetSourceString, ZEND_RETURN_VALUE, 0, V8\\StringValue, 0)
108+
ZEND_END_ARG_INFO()
109+
110+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_source_GetScriptOrigin, ZEND_RETURN_VALUE, 0, V8\\ScriptOrigin, 1)
111+
ZEND_END_ARG_INFO()
112+
113+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_v8_source_GetCachedData, ZEND_RETURN_VALUE, 0, V8\\ScriptCompiler\\CachedData, 1)
114+
ZEND_END_ARG_INFO()
115+
116+
117+
static const zend_function_entry php_v8_source_methods[] = {
118+
PHP_ME(V8Source, __construct, arginfo_v8_source___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
119+
PHP_ME(V8Source, GetSourceString, arginfo_v8_source_GetSourceString, ZEND_ACC_PUBLIC)
120+
PHP_ME(V8Source, GetScriptOrigin, arginfo_v8_source_GetScriptOrigin, ZEND_ACC_PUBLIC)
121+
PHP_ME(V8Source, GetCachedData, arginfo_v8_source_GetCachedData, ZEND_ACC_PUBLIC)
122+
123+
PHP_FE_END
124+
};
125+
126+
127+
PHP_MINIT_FUNCTION(php_v8_source)
128+
{
129+
zend_class_entry ce;
130+
131+
INIT_NS_CLASS_ENTRY(ce, "V8\\ScriptCompiler", "Source", php_v8_source_methods);
132+
this_ce = zend_register_internal_class(&ce);
133+
134+
zend_declare_property_null(this_ce, ZEND_STRL("source_string"), ZEND_ACC_PRIVATE);
135+
zend_declare_property_null(this_ce, ZEND_STRL("origin"), ZEND_ACC_PRIVATE);
136+
zend_declare_property_null(this_ce, ZEND_STRL("cached_data"), ZEND_ACC_PRIVATE);
137+
138+
return SUCCESS;
139+
}

src/php_v8_source.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the pinepain/php-v8 PHP extension.
3+
*
4+
* Copyright (c) 2015-2017 Bogdan Padalko <pinepain@gmail.com>
5+
*
6+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
7+
*
8+
* For the full copyright and license information, please view the
9+
* LICENSE file that was distributed with this source or visit
10+
* http://opensource.org/licenses/MIT
11+
*/
12+
13+
#ifndef PHP_V8_SOURCE_H
14+
#define PHP_V8_SOURCE_H
15+
16+
17+
#include "php_v8_exception.h"
18+
#include "php_v8_context.h"
19+
#include "php_v8_isolate.h"
20+
#include <v8.h>
21+
22+
extern "C" {
23+
#include "php.h"
24+
25+
#ifdef ZTS
26+
#include "TSRM.h"
27+
#endif
28+
}
29+
30+
extern zend_class_entry *php_v8_source_class_entry;
31+
32+
extern void php_v8_update_source_cached_data(zval *src_zv, v8::ScriptCompiler::Source *source);
33+
34+
#define PHP_V8_SOURCE_READ_SOURCE_STRING(from_zval) zend_read_property(php_v8_source_class_entry, (from_zval), ZEND_STRL("source_string"), 0, &rv)
35+
#define PHP_V8_SOURCE_READ_ORIGIN(from_zval) zend_read_property(php_v8_source_class_entry, (from_zval), ZEND_STRL("origin"), 0, &rv)
36+
#define PHP_V8_SOURCE_READ_CACHED_DATA(from_zval) zend_read_property(php_v8_source_class_entry, (from_zval), ZEND_STRL("cached_data"), 0, &rv)
37+
38+
PHP_MINIT_FUNCTION(php_v8_source);
39+
40+
#endif //PHP_V8_SOURCE_H

stubs/src/ScriptCompiler/Source.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace V8\ScriptCompiler;
4+
5+
use V8\ScriptOrigin;
6+
use V8\StringValue;
7+
8+
9+
/**
10+
* Source code which can be then compiled to a UnboundScript or Script.
11+
*/
12+
class Source
13+
{
14+
/**
15+
* @var StringValue
16+
*/
17+
private $source_string;
18+
/**
19+
* @var null|ScriptOrigin
20+
*/
21+
private $origin;
22+
/**
23+
* @var null|CachedData
24+
*/
25+
private $cached_data;
26+
27+
/**
28+
* @param StringValue $source_string
29+
* @param ScriptOrigin|null $origin
30+
* @param CachedData|null $cached_data
31+
*/
32+
public function __construct(StringValue $source_string, ScriptOrigin $origin = null, CachedData $cached_data = null)
33+
{
34+
$this->source_string = $source_string;
35+
$this->origin = $origin;
36+
$this->cached_data = $cached_data;
37+
}
38+
39+
public function GetSourceString(): StringValue
40+
{
41+
return $this->source_string;
42+
}
43+
44+
public function GetScriptOrigin(): ScriptOrigin
45+
{
46+
return $this->origin;
47+
}
48+
49+
public function GetCachedData(): CachedData
50+
{
51+
return $this->cached_data;
52+
}
53+
}

0 commit comments

Comments
 (0)