本文整理汇总了C++中js::CompartmentOptions::setSourcePolicy方法的典型用法代码示例。如果您正苦于以下问题:C++ CompartmentOptions::setSourcePolicy方法的具体用法?C++ CompartmentOptions::setSourcePolicy怎么用?C++ CompartmentOptions::setSourcePolicy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类js::CompartmentOptions
的用法示例。
在下文中一共展示了CompartmentOptions::setSourcePolicy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CompileFile
bool CompileFile(const std::string &inputFilePath, const std::string &outputFilePath) {
bool result = false;
std::string ofp;
if (!outputFilePath.empty()) {
ofp = outputFilePath;
}
else {
ofp = RemoveFileExt(inputFilePath) + BYTE_CODE_FILE_EXT;
}
if (!JS_Init())
return false;
std::cout << "Input file: " << inputFilePath << std::endl;
JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS);
JSContext *cx = JS_NewContext(runtime, 10240);
JS_SetOptions(cx, JSOPTION_TYPE_INFERENCE);
JS::CompartmentOptions options;
options.setVersion(JSVERSION_LATEST);
JS::RootedObject global(cx, JS_NewGlobalObject(cx, &GlobalClass, NULL, JS::DontFireOnNewGlobalHook, options));
JS_SetErrorReporter(cx, &ReportError);
{
JSAutoCompartment ac(cx, global);
if (JS_InitStandardClasses(cx, global)) {
JS_InitReflect(cx, global);
JS_FireOnNewGlobalObject(cx, global);
JS::CompileOptions options(cx);
options.setUTF8(true);
options.setSourcePolicy(JS::CompileOptions::NO_SOURCE);
std::cout << "Compiling ..." << std::endl;
JS::RootedScript script(cx, JS::Compile(cx, global, options, inputFilePath.c_str()));
if (script) {
void *data = NULL;
uint32_t length = 0;
std::cout << "Encoding ..." << std::endl;
data = JS_EncodeScript(cx, script, &length);
if (data) {
if (WriteFile(ofp, data, length)) {
std::cout << "Done! " << "Output file: " << ofp << std::endl;
result = true;
}
}
}
else
{
std::cout << "Compiled " << inputFilePath << " fails!" << std::endl;
}
}
else
{
std::cout << "JS_InitStandardClasses failed! " << std::endl;
}
}
if (cx) {
JS_DestroyContext(cx);
cx = NULL;
}
if (runtime) {
JS_DestroyRuntime(runtime);
runtime = NULL;
}
JS_ShutDown();
return result;
}