Skip to content

Commit 61f4c22

Browse files
hashseedCommit Bot
authored andcommitted
Assume flat string when checking CompileFunctionInContext arguments.
[email protected] Change-Id: I54c6137a3c6e14d4102188f154aa7216e7414dbc Reviewed-on: https://chromium-review.googlesource.com/c/1388533 Reviewed-by: Jakob Kummerow <[email protected]> Commit-Queue: Yang Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{#58562}
1 parent 638d1b3 commit 61f4c22

2 files changed

Lines changed: 30 additions & 45 deletions

File tree

src/api.cc

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,43 +2432,28 @@ MaybeLocal<Module> ScriptCompiler::CompileModule(
24322432
return ToApiHandle<Module>(i_isolate->factory()->NewModule(shared));
24332433
}
24342434

2435-
2436-
class IsIdentifierHelper {
2437-
public:
2438-
IsIdentifierHelper() : is_identifier_(false), first_char_(true) {}
2439-
2440-
bool Check(i::String string) {
2441-
i::ConsString cons_string = i::String::VisitFlat(this, string, 0);
2442-
if (cons_string.is_null()) return is_identifier_;
2443-
// We don't support cons strings here.
2444-
return false;
2445-
}
2446-
void VisitOneByteString(const uint8_t* chars, int length) {
2447-
for (int i = 0; i < length; ++i) {
2448-
if (first_char_) {
2449-
first_char_ = false;
2450-
is_identifier_ = i::IsIdentifierStart(chars[0]);
2451-
} else {
2452-
is_identifier_ &= i::IsIdentifierPart(chars[i]);
2453-
}
2435+
namespace {
2436+
bool IsIdentifier(i::Isolate* isolate, i::Handle<i::String> string) {
2437+
string = i::String::Flatten(isolate, string);
2438+
const int length = string->length();
2439+
if (length == 0) return false;
2440+
if (!i::IsIdentifierStart(string->Get(0))) return false;
2441+
i::DisallowHeapAllocation no_gc;
2442+
i::String::FlatContent flat = string->GetFlatContent(no_gc);
2443+
if (flat.IsOneByte()) {
2444+
auto vector = flat.ToOneByteVector();
2445+
for (int i = 1; i < length; i++) {
2446+
if (!i::IsIdentifierPart(vector[i])) return false;
24542447
}
2455-
}
2456-
void VisitTwoByteString(const uint16_t* chars, int length) {
2457-
for (int i = 0; i < length; ++i) {
2458-
if (first_char_) {
2459-
first_char_ = false;
2460-
is_identifier_ = i::IsIdentifierStart(chars[0]);
2461-
} else {
2462-
is_identifier_ &= i::IsIdentifierPart(chars[i]);
2463-
}
2448+
} else {
2449+
auto vector = flat.ToUC16Vector();
2450+
for (int i = 1; i < length; i++) {
2451+
if (!i::IsIdentifierPart(vector[i])) return false;
24642452
}
24652453
}
2466-
2467-
private:
2468-
bool is_identifier_;
2469-
bool first_char_;
2470-
DISALLOW_COPY_AND_ASSIGN(IsIdentifierHelper);
2471-
};
2454+
return true;
2455+
}
2456+
} // anonymous namespace
24722457

24732458
MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
24742459
Local<Context> v8_context, Source* source, size_t arguments_count,
@@ -2493,9 +2478,8 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
24932478
i::Handle<i::FixedArray> arguments_list =
24942479
isolate->factory()->NewFixedArray(static_cast<int>(arguments_count));
24952480
for (int i = 0; i < static_cast<int>(arguments_count); i++) {
2496-
IsIdentifierHelper helper;
24972481
i::Handle<i::String> argument = Utils::OpenHandle(*arguments[i]);
2498-
if (!helper.Check(*argument)) return Local<Function>();
2482+
if (!IsIdentifier(isolate, argument)) return Local<Function>();
24992483
arguments_list->set(i, *argument);
25002484
}
25012485

test/cctest/test-compiler.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ TEST(CompileFunctionInContextArgs) {
486486
v8::Local<v8::Object> ext[1];
487487
ext[0] = v8::Local<v8::Object>::Cast(
488488
env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
489-
v8::ScriptCompiler::Source script_source(v8_str("result = x + b"));
490-
v8::Local<v8::String> arg = v8_str("b");
489+
v8::ScriptCompiler::Source script_source(v8_str("result = x + abc"));
490+
v8::Local<v8::String> arg = v8_str("abc");
491491
v8::Local<v8::Function> fun =
492492
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
493493
1, &arg, 1, ext)
@@ -497,8 +497,8 @@ TEST(CompileFunctionInContextArgs) {
497497
->ToInt32(env.local())
498498
.ToLocalChecked()
499499
->Value());
500-
v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
501-
fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
500+
v8::Local<v8::Value> arg_value = v8::Number::New(CcTest::isolate(), 42.0);
501+
fun->Call(env.local(), env->Global(), 1, &arg_value).ToLocalChecked();
502502
CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
503503
v8::Local<v8::Value> result =
504504
env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
@@ -515,16 +515,17 @@ TEST(CompileFunctionInContextComments) {
515515
v8::Local<v8::Object> ext[1];
516516
ext[0] = v8::Local<v8::Object>::Cast(
517517
env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked());
518-
v8::ScriptCompiler::Source script_source(
519-
v8_str("result = /* y + */ x + b // + z"));
520-
v8::Local<v8::String> arg = v8_str("b");
518+
v8::Local<v8::String> source =
519+
CompileRun("'result = /* y + */ x + a\\u4e00 // + z'").As<v8::String>();
520+
v8::ScriptCompiler::Source script_source(source);
521+
v8::Local<v8::String> arg = CompileRun("'a\\u4e00'").As<v8::String>();
521522
v8::Local<v8::Function> fun =
522523
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
523524
1, &arg, 1, ext)
524525
.ToLocalChecked();
525526
CHECK(!fun.IsEmpty());
526-
v8::Local<v8::Value> b_value = v8::Number::New(CcTest::isolate(), 42.0);
527-
fun->Call(env.local(), env->Global(), 1, &b_value).ToLocalChecked();
527+
v8::Local<v8::Value> arg_value = v8::Number::New(CcTest::isolate(), 42.0);
528+
fun->Call(env.local(), env->Global(), 1, &arg_value).ToLocalChecked();
528529
CHECK(env->Global()->Has(env.local(), v8_str("result")).FromJust());
529530
v8::Local<v8::Value> result =
530531
env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();

0 commit comments

Comments
 (0)