Пример #1
0
// Executes a string within the current v8 context.
bool Fragment::Script::ScriptEngine::ExecuteString(v8::Isolate *isolate, v8::Local<v8::String> source,
                   v8::Local<v8::Value> name, bool print_result,
                   bool report_exceptions) {
    v8::HandleScope handle_scope(isolate);
    v8::TryCatch try_catch(isolate);
    v8::ScriptOrigin origin(name);
    v8::Local<v8::Context> context(isolate->GetCurrentContext());
    v8::Local<v8::Script> script;
    if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) {
        // Print errors that happened during compilation.
        if (report_exceptions)
            ReportException(isolate, &try_catch);
        return false;
    } else {
        v8::Local<v8::Value> result;
        if (!script->Run(context).ToLocal(&result)) {
            assert(try_catch.HasCaught());
            // Print errors that happened during execution.
            if (report_exceptions)
                ReportException(isolate, &try_catch);
            return false;
        } else {
            assert(!try_catch.HasCaught());
            if (print_result && !result->IsUndefined()) {
                // If all went well and the result wasn't undefined then print
                // the returned value.
                v8::String::Utf8Value str(result);
                const char *cstr = ToCString(str);
                printf("%s\n", cstr);
            }
            return true;
        }
    }
}
Пример #2
0
int FXJS_Execute(v8::Isolate* pIsolate,
                 IJS_Context* pJSContext,
                 const wchar_t* script,
                 FXJSErr* pError) {
  v8::Isolate::Scope isolate_scope(pIsolate);
  v8::TryCatch try_catch(pIsolate);
  CFX_ByteString bsScript = CFX_WideString(script).UTF8Encode();
  v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
  v8::Local<v8::Script> compiled_script;
  if (!v8::Script::Compile(
           context, v8::String::NewFromUtf8(
                        pIsolate, bsScript.c_str(), v8::NewStringType::kNormal,
                        bsScript.GetLength()).ToLocalChecked())
           .ToLocal(&compiled_script)) {
    v8::String::Utf8Value error(try_catch.Exception());
    // TODO(tsepez): return error via pError->message.
    return -1;
  }

  v8::Local<v8::Value> result;
  if (!compiled_script->Run(context).ToLocal(&result)) {
    v8::String::Utf8Value error(try_catch.Exception());
    // TODO(tsepez): return error via pError->message.
    return -1;
  }
  return 0;
}
Пример #3
0
bool JsHttpRequestProcessor::ExecuteScript(Local<String> script) {
  HandleScope handle_scope(GetIsolate());

  // We're just about to compile the script; set up an error handler to
  // catch any exceptions the script might throw.
  TryCatch try_catch(GetIsolate());

  Local<Context> context(GetIsolate()->GetCurrentContext());

  // Compile the script and check for errors.
  Local<Script> compiled_script;
  if (!Script::Compile(context, script).ToLocal(&compiled_script)) {
    String::Utf8Value error(try_catch.Exception());
    Log(*error);
    // The script failed to compile; bail out.
    return false;
  }

  // Run the script!
  Local<Value> result;
  if (!compiled_script->Run(context).ToLocal(&result)) {
    // The TryCatch above is still in effect and will have caught the error.
    String::Utf8Value error(try_catch.Exception());
    Log(*error);
    // Running the script failed; bail out.
    return false;
  }
  return true;
}
Пример #4
0
bool JsHttpRequestProcessor::Process(HttpRequest* request) {
  // Create a handle scope to keep the temporary object references.
  HandleScope handle_scope(GetIsolate());

  v8::Local<v8::Context> context =
      v8::Local<v8::Context>::New(GetIsolate(), context_);

  // Enter this processor's context so all the remaining operations
  // take place there
  Context::Scope context_scope(context);

  // Wrap the C++ request object in a JavaScript wrapper
  Local<Object> request_obj = WrapRequest(request);

  // Set up an exception handler before calling the Process function
  TryCatch try_catch(GetIsolate());

  // Invoke the process function, giving the global object as 'this'
  // and one argument, the request.
  const int argc = 1;
  Local<Value> argv[argc] = {request_obj};
  v8::Local<v8::Function> process =
      v8::Local<v8::Function>::New(GetIsolate(), process_);
  Local<Value> result;
  if (!process->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
    String::Utf8Value error(try_catch.Exception());
    Log(*error);
    return false;
  } else {
    return true;
  }
}
Пример #5
0
int getCertChain(
    ServiceConnection & serviceConnection,
    LogicCommand command,
    int counter,
    const CertificateShPtr &certificate,
    const T &untrustedVector,
    const T &trustedVector,
    bool useTrustedSystemCertificates,
    CertificateShPtrVector &certificateChainVector)
{
    return try_catch([&] {
        MessageBuffer recv;
        auto send = MessageBuffer::Serialize(static_cast<int>(command),
                                             counter,
                                             certificate->getDER(),
                                             untrustedVector,
                                             trustedVector,
                                             useTrustedSystemCertificates);

        int retCode = serviceConnection.processRequest(send.Pop(), recv);
        if (CKM_API_SUCCESS != retCode)
            return retCode;

        int retCommand;
        int retCounter;
        RawBufferVector rawBufferVector;
        recv.Deserialize(retCommand, retCounter, retCode, rawBufferVector);

        if ((counter != retCounter) || (static_cast<int>(command) != retCommand))
            return CKM_API_ERROR_UNKNOWN;

        if (retCode != CKM_API_SUCCESS)
            return retCode;

        for (auto &e: rawBufferVector) {
            CertificateShPtr cert(new CertificateImpl(e, DataFormat::FORM_DER));
            if (cert->empty())
                return CKM_API_ERROR_BAD_RESPONSE;
            certificateChainVector.push_back(cert);
        }

        return retCode;
    });
}
Пример #6
0
	Status PreludeScript::get_template(std::vector<v8::Handle<v8::Value> > &prelude_arguments, v8::Handle<v8::ObjectTemplate> &result)
	{
		v8::Isolate::Scope isolate_scope(get_isolate());
		v8::Context::Scope context_scope(get_context());
		v8::Handle<v8::Object> global = get_context()->Global();
		v8::Handle<v8::Value> prelude_result;
		v8::Handle<v8::Object> prelude_result_object;
		v8::TryCatch try_catch(isolate);

		if (!enter_cancellable_region()) 
			return S_TERMINATED; // initialized with 0 by default
		v8::Handle<v8::Function> global_template_factory_local = v8::Handle<v8::Function>::New(get_isolate(), *global_template_factory);
		prelude_result = global_template_factory_local->Call(global, (int)prelude_arguments.size(), prelude_arguments.data());
		if (!exit_cancellable_region())
			return S_TERMINATED; // initialized with 0 by default

		if (set_last_error(get_isolate(), prelude_result.IsEmpty(), try_catch))
			return S_ERROR;
		if (prelude_result.IsEmpty())
		{
			set_last_error(get_isolate(), "Global template factory did not return any value");
			return S_ERROR; // initialized with 0 by default
		}
		if (!prelude_result->IsObject()) 
		{
			set_last_error(get_isolate(), "Prelude script must return a function");
			return S_ERROR; // initialized with 0 by default
		}

		prelude_result_object = prelude_result.As<v8::Object>();
		v8::Handle<v8::Array> global_property_names = prelude_result_object->GetPropertyNames();

		for (unsigned int i = 0; i < global_property_names->Length(); i++) 
		{
			v8::Handle<v8::String> global_property_name = global_property_names->Get(i).As<v8::String>();
			v8::Handle<v8::Value> global_property_value = prelude_result_object->Get(global_property_name);

			v8::String::Utf8Value name(get_isolate(),global_property_name);
			v8::String::Utf8Value value(get_isolate(),global_property_value);
			global->Set(global_property_name, global_property_value);
		}

		return S_OK;
	}
Пример #7
0
void CJS_Value::MaybeCoerceToNumber() {
  bool bAllowNaN = false;
  if (m_eType == VT_string) {
    CFX_ByteString bstr = ToCFXByteString();
    if (bstr.GetLength() == 0)
      return;
    if (bstr == "NaN")
      bAllowNaN = true;
  }
  v8::TryCatch try_catch(m_pJSRuntime->GetIsolate());
  v8::MaybeLocal<v8::Number> maybeNum =
      m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext());
  if (maybeNum.IsEmpty())
    return;
  v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
  if (std::isnan(num->Value()) && !bAllowNaN)
    return;
  m_pValue = num;
  m_eType = VT_number;
}
Пример #8
0
	Local<Value> ScriptEngine::ExecuteScript(const char * scripts)
	{
		Isolate::Scope isolate_scope(getIsoloate());
		HandleScope handle_scope(getIsoloate());
		Local<Context> context = Local<Context>::New(getIsoloate(), context_);
		Context::Scope context_scope(context);
		Local<Value> result;
		TryCatch try_catch(getIsoloate());
		Local<Script> compiled_script;
		if (!Script::Compile(context, String::NewFromUtf8(getIsoloate(), scripts)).ToLocal(&compiled_script)) {
			String::Utf8Value error(try_catch.Exception());
			return result;
		}
		// Run the script!
		auto val = compiled_script->Run(context);
		if (!val.ToLocal(&result)) {
			String::Utf8Value error(try_catch.Exception());
			return result;
		}
		String::Utf8Value ret(result);
		KLOG(Info, "V8Script", "---- ExecuteScript %s", *ret);
		return result;
	}