-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vm: add code generation options #19016
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,30 @@ Local<Context> ContextifyContext::CreateV8Context( | |
CHECK(name->IsString()); | ||
Utf8Value name_val(env->isolate(), name); | ||
|
||
Local<Value> codegen = options_obj->Get(env->context(), | ||
FIXED_ONE_BYTE_STRING(env->isolate(), "codeGeneration")) | ||
.ToLocalChecked(); | ||
|
||
if (!codegen->IsUndefined()) { | ||
CHECK(codegen->IsObject()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could leave a comment here, that the js part already ensured that we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this pattern it used a lot with stuff that is validated js-side and we don't leave usually comments, are you worried about something specific not being construed? |
||
Local<Object> codegen_obj = codegen.As<Object>(); | ||
|
||
Local<Value> allow_code_gen_from_strings = | ||
codegen_obj->Get(env->context(), | ||
FIXED_ONE_BYTE_STRING(env->isolate(), "strings")) | ||
.ToLocalChecked(); | ||
ctx->AllowCodeGenerationFromStrings( | ||
allow_code_gen_from_strings->IsUndefined() || | ||
allow_code_gen_from_strings->IsTrue()); | ||
|
||
Local<Value> allow_wasm_code_gen = codegen_obj->Get(env->context(), | ||
FIXED_ONE_BYTE_STRING(env->isolate(), "wasm")) | ||
.ToLocalChecked(); | ||
ctx->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration, | ||
Boolean::New(env->isolate(), allow_wasm_code_gen->IsUndefined() || | ||
allow_wasm_code_gen->IsTrue())); | ||
} | ||
|
||
ContextInfo info(*name_val); | ||
|
||
Local<Value> origin = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
'use strict'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also needs tests for unexpected types of |
||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { createContext, runInContext, runInNewContext } = require('vm'); | ||
|
||
const WASM_BYTES = Buffer.from( | ||
[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); | ||
|
||
|
||
function expectsError(fn, type) { | ||
try { | ||
fn(); | ||
assert.fail('expected fn to error'); | ||
} catch (err) { | ||
if (typeof type === 'string') | ||
assert.strictEqual(err.name, type); | ||
else | ||
assert(err instanceof type); | ||
} | ||
} | ||
|
||
{ | ||
const ctx = createContext({ WASM_BYTES }); | ||
const test = 'eval(""); new WebAssembly.Module(WASM_BYTES);'; | ||
runInContext(test, ctx); | ||
|
||
runInNewContext(test, { WASM_BYTES }, { | ||
contextCodeGeneration: undefined, | ||
}); | ||
} | ||
|
||
{ | ||
const ctx = createContext({}, { | ||
codeGeneration: { | ||
strings: false, | ||
}, | ||
}); | ||
|
||
const EvalError = runInContext('EvalError', ctx); | ||
expectsError(() => { | ||
runInContext('eval("x")', ctx); | ||
}, EvalError); | ||
} | ||
|
||
{ | ||
const ctx = createContext({ WASM_BYTES }, { | ||
codeGeneration: { | ||
wasm: false, | ||
}, | ||
}); | ||
|
||
const CompileError = runInContext('WebAssembly.CompileError', ctx); | ||
expectsError(() => { | ||
runInContext('new WebAssembly.Module(WASM_BYTES)', ctx); | ||
}, CompileError); | ||
} | ||
|
||
expectsError(() => { | ||
runInNewContext('eval("x")', {}, { | ||
contextCodeGeneration: { | ||
strings: false, | ||
}, | ||
}); | ||
}, 'EvalError'); | ||
|
||
expectsError(() => { | ||
runInNewContext('new WebAssembly.Module(WASM_BYTES)', { WASM_BYTES }, { | ||
contextCodeGeneration: { | ||
wasm: false, | ||
}, | ||
}); | ||
}, 'CompileError'); | ||
|
||
common.expectsError(() => { | ||
createContext({}, { | ||
codeGeneration: { | ||
strings: 0, | ||
}, | ||
}); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
|
||
common.expectsError(() => { | ||
runInNewContext('eval("x")', {}, { | ||
contextCodeGeneration: { | ||
wasm: 1, | ||
}, | ||
}); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
|
||
common.expectsError(() => { | ||
createContext({}, { | ||
codeGeneration: 1, | ||
}); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
|
||
common.expectsError(() => { | ||
createContext({}, { | ||
codeGeneration: null, | ||
}); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rather name this function
validateObjectOrUndefined
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
none of the others are named like that, and it seems fairly self-explanatory