Skip to content

Commit fd3246b

Browse files
committed
* Added V8Js::FLAG_CHECK_BINARY_STRING
1 parent 91fdb85 commit fd3246b

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

php_v8js_macros.h

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extern "C" {
7777
#define V8JS_FLAG_NONE (1<<0)
7878
#define V8JS_FLAG_FORCE_ARRAY (1<<1)
7979
#define V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS (1<<2)
80+
#define V8JS_FLAG_CHECK_BINARY_STRING (1<<3)
8081

8182

8283
/* These are not defined by Zend */

v8js_class.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ PHP_MINIT_FUNCTION(v8js_class) /* {{{ */
10801080
zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE);
10811081
zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY);
10821082
zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_PROPAGATE_PHP_EXCEPTIONS"), V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS);
1083+
zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_CHECK_BINARY_STRING"), V8JS_FLAG_CHECK_BINARY_STRING);
10831084

10841085
le_v8js_script = zend_register_list_destructors_ex(v8js_script_dtor, NULL, PHP_V8JS_SCRIPT_RES_NAME, module_number);
10851086

v8js_convert.cc

+27-24
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ v8::Local<v8::Value> zend_long_to_v8js(zend_long v, v8::Isolate *isolate) /* {{{
110110

111111
v8::Local<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate) /* {{{ */
112112
{
113+
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
113114
v8::Local<v8::Value> jsValue;
114115
zend_string *value_str;
115116
zend_class_entry *ce;
@@ -150,19 +151,21 @@ v8::Local<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate) /* {{{ */
150151
break;
151152
}
152153

153-
zval fname, retval;
154-
zval params[2];
155-
ZVAL_STRING(&fname, "mb_check_encoding");
156-
ZVAL_COPY_VALUE(&params[0], value);
157-
ZVAL_STRING(&params[1], "UTF-8");
158-
159-
if ((SUCCESS == call_user_function(CG(function_table), NULL, &fname,
160-
&retval, 2, params)) && (Z_TYPE(retval) == IS_TRUE)) {
161-
jsValue = V8JS_ZSTR(value_str);
162-
}
163-
else {
164-
jsValue = v8::String::NewFromOneByte(isolate, (unsigned char *)ZSTR_VAL(value_str), v8::NewStringType::kNormal, ZSTR_LEN(value_str)).ToLocalChecked();
154+
if (ctx->flags & V8JS_FLAG_CHECK_BINARY_STRING) {
155+
zval fname, retval;
156+
zval params[2];
157+
ZVAL_STRING(&fname, "mb_check_encoding");
158+
ZVAL_COPY_VALUE(&params[0], value);
159+
ZVAL_STRING(&params[1], "UTF-8");
160+
if ((SUCCESS == call_user_function(CG(function_table), NULL, &fname,
161+
&retval, 2, params)) && (Z_TYPE(retval) == IS_FALSE)) {
162+
163+
jsValue = v8::String::NewFromOneByte(isolate, (unsigned char *)ZSTR_VAL(value_str), v8::NewStringType::kNormal, ZSTR_LEN(value_str)).ToLocalChecked();
164+
return jsValue;
165+
}
165166
}
167+
168+
jsValue = V8JS_ZSTR(value_str);
166169

167170
break;
168171

@@ -204,19 +207,19 @@ int v8js_to_zval(v8::Local<v8::Value> jsValue, zval *return_value, int flags, v8
204207

205208
if (jsValue->IsString())
206209
{
207-
v8::Local<v8::String> strOneByte = jsValue->ToString(v8_context).ToLocalChecked();
208-
if (strOneByte->IsOneByte() && strOneByte->ContainsOnlyOneByte()) {
209-
int length = strOneByte->Length();
210-
char* char_value = new char[length + 1];
211-
strOneByte->WriteOneByte(isolate, reinterpret_cast<uint8_t*>(&char_value[0]), 0, length);
212-
213-
RETVAL_STRINGL(char_value, length);
214-
}
215-
else {
216-
v8::String::Utf8Value str(isolate, jsValue);
217-
const char *cstr = ToCString(str);
218-
RETVAL_STRINGL(cstr, str.length());
210+
if (ctx->flags & V8JS_FLAG_CHECK_BINARY_STRING) {
211+
v8::Local<v8::String> strOneByte = jsValue->ToString(v8_context).ToLocalChecked();
212+
if (strOneByte->ContainsOnlyOneByte()) {
213+
int length = strOneByte->Length();
214+
char* char_value = new char[length + 1];
215+
strOneByte->WriteOneByte(isolate, reinterpret_cast<uint8_t*>(&char_value[0]), 0, length);
216+
RETVAL_STRINGL(char_value, length);
217+
return SUCCESS;
218+
}
219219
}
220+
v8::String::Utf8Value str(isolate, jsValue);
221+
const char *cstr = ToCString(str);
222+
RETVAL_STRINGL(cstr, str.length());
220223
}
221224
else if (jsValue->IsBoolean())
222225
{

0 commit comments

Comments
 (0)