コード例 #1
0
ファイル: fibjs.cpp プロジェクト: java10000/fibjs
void _main(const char *fname)
{
    v8::V8::Initialize();

    v8::Platform *platform = v8::platform::CreateDefaultPlatform();
    v8::V8::InitializePlatform(platform);

    isolate = v8::Isolate::New();
    v8::Locker locker(isolate);
    v8::Isolate::Scope isolate_scope(isolate);

    v8::HandleScope handle_scope(isolate);

    v8::Local<v8::Context> _context = v8::Context::New(isolate);
    v8::Context::Scope context_scope(_context);

    v8::Local<v8::Object> glob = _context->Global();
    global_base::class_info().Attach(glob, NULL);

    Function_base::class_info().Attach(
        glob->Get(v8::String::NewFromUtf8(isolate, "Function"))->ToObject()->GetPrototype()->ToObject(),
        NULL);

    s_context.Reset(isolate, _context);
    s_global.Reset(isolate, glob);

    JSFiber *fb = new JSFiber();
    {
        JSFiber::scope s(fb);
        s_topSandbox = new SandBox();

        s_topSandbox->initRoot();
        if (fname)
            s.m_hr = s_topSandbox->run(fname);
        else
            s.m_hr = s_topSandbox->repl();
    }

    process_base::exit(0);

    isolate->Dispose();

    v8::V8::ShutdownPlatform();
    delete platform;

    s_context.Reset();
}
v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root)
{
    static v8::Persistent<v8::FunctionTemplate> npObjectDesc;

    ASSERT(v8::Context::InContext());

    v8::Isolate* isolate = v8::Isolate::GetCurrent();

    // If this is a v8 object, just return it.
    V8NPObject* v8NPObject = npObjectToV8NPObject(object);
    if (v8NPObject)
        return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);

    // If we've already wrapped this object, just return it.
    v8::Handle<v8::Object> wrapper = staticNPObjectMap().get(object);
    if (!wrapper.IsEmpty())
        return v8::Local<v8::Object>::New(isolate, wrapper);

    // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped
    // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type
    // can be used by DOM bindings.
    if (npObjectDesc.IsEmpty()) {
        v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New();
        templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount);
        templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedPropertyEnumerator);
        templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator);
        templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler);
        npObjectDesc.Reset(isolate, templ);
    }

    // FIXME: Move staticNPObjectMap() to DOMDataStore.
    // Use V8DOMWrapper::createWrapper() and
    // V8DOMWrapper::associateObjectWithWrapper()
    // to create a wrapper object.
    v8::Handle<v8::Function> v8Function = v8::Local<v8::FunctionTemplate>::New(isolate, npObjectDesc)->GetFunction();
    v8::Local<v8::Object> value = V8ObjectConstructor::newInstance(v8Function);
    if (value.IsEmpty())
        return value;

    V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), object);

    // KJS retains the object as part of its wrapper (see Bindings::CInstance).
    _NPN_RetainObject(object);
    _NPN_RegisterObject(object, root);

    WrapperConfiguration configuration = buildWrapperConfiguration(object, WrapperConfiguration::Dependent);
    staticNPObjectMap().set(object, value, configuration);
    ASSERT(V8DOMWrapper::maybeDOMWrapper(value));
    return value;
}
コード例 #3
0
ファイル: fxjs_v8.cpp プロジェクト: mcanthony/libpdf
void JS_InitialRuntime(IJS_Runtime* pJSRuntime,IFXJS_Runtime* pFXRuntime, IFXJS_Context* context, v8::Persistent<v8::Context>& v8PersistentContext)
{
	v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
	v8::Isolate::Scope isolate_scope(isolate);
	v8::HandleScope handle_scope(isolate);

	v8::Persistent<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
	v8::Handle<v8::Context> v8Context = v8::Context::New(isolate, NULL, v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp));
	v8::Context::Scope context_scope(v8Context);

	v8::Handle<v8::External> ptr = v8::External::New(isolate, pFXRuntime);
	v8Context->SetEmbedderData(1, ptr);

	CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
	if(!pArray) return;

	for(int i=0; i<pArray->GetSize(); i++)
	{
		CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
		CFX_WideString ws = CFX_WideString(pObjDef->objName);
		CFX_ByteString bs = ws.UTF8Encode();
		v8::Handle<v8::String> objName = v8::String::NewFromUtf8(isolate, bs.c_str(), v8::String::kNormalString, bs.GetLength());


		if(pObjDef->objType == JS_DYNAMIC)
		{
			//Document is set as global object, need to construct it first.
			if(ws.Equal(L"Document"))
			{

				CJS_PrivateData* pPrivateData = new CJS_PrivateData;
				pPrivateData->ObjDefID = i;

				v8Context->Global()->GetPrototype()->ToObject()->SetAlignedPointerInInternalField(0, pPrivateData);

				if(pObjDef->m_pConstructor)
					pObjDef->m_pConstructor(context, v8Context->Global()->GetPrototype()->ToObject(), v8Context->Global()->GetPrototype()->ToObject());
			}
		}
		else
		{
			v8::Handle<v8::Object> obj = JS_NewFxDynamicObj(pJSRuntime, context, i);
			v8Context->Global()->Set(objName, obj);
			pObjDef->m_StaticObj.Reset(isolate, obj);
		}
	}
	v8PersistentContext.Reset(isolate, v8Context);
}
コード例 #4
0
v8::Persistent<v8::FunctionTemplate> V8TestNamedConstructorConstructor::GetTemplate(v8::Isolate* isolate, WrapperWorldType currentWorldType)
{
    static v8::Persistent<v8::FunctionTemplate> cachedTemplate;
    if (!cachedTemplate.IsEmpty())
        return cachedTemplate;

    v8::HandleScope scope;
    v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(V8TestNamedConstructorConstructorCallback);

    v8::Local<v8::ObjectTemplate> instance = result->InstanceTemplate();
    instance->SetInternalFieldCount(V8TestNamedConstructor::internalFieldCount);
    result->SetClassName(v8::String::NewSymbol("TestNamedConstructor"));
    result->Inherit(V8TestNamedConstructor::GetTemplate(isolate, currentWorldType));

    cachedTemplate.Reset(isolate, result);
    return cachedTemplate;
}
コード例 #5
0
void ScriptJSApp::setup(){
    mScriptContext.addModule(new ModuleClassCpp());
    mScriptContext.addModule(new GL());
    mScriptContext.loadScript("/Users/automat/Projects/next/ScriptJS/resources/script.js");
    
    mCamera.setPerspective(45.0f, app::getWindowAspectRatio(), 0.0001f, 4);
    mCameraEye.set(2,2,2);
    mCameraTarget.set(0, 0, 0);
    mCamera.lookAt(mCameraEye, mCameraTarget);
    
    ENTER_CONTEXT(mScriptContext);
    const int argc = 2;
    Handle<Value> argv[argc] = {scriptjs::ToV8Int(app::getWindowWidth()),
                                scriptjs::ToV8Int(app::getWindowHeight())};
    mContextJS.Reset(isolate, mScriptContext.newInstance("ContextJS",argc,argv));
    EXIT_CONTEXT;
    
}
コード例 #6
0
v8::Local<v8::Function>
make_function(F f, CallPolicies const& p, Sig signature) {
    static v8::Persistent<v8::FunctionTemplate> proto_template;
    v8::Local<v8::FunctionTemplate> pt;
    if (proto_template.IsEmpty()) {
        pt = v8::FunctionTemplate::New(v8::Isolate::GetCurrent());
        pt->InstanceTemplate()->SetInternalFieldCount(1);
        proto_template.Reset(v8::Isolate::GetCurrent(), pt);
    } else {
        pt = v8::Local<v8::FunctionTemplate>::New(v8::Isolate::GetCurrent(), proto_template);
    }
    typedef typename detail::caller<F, CallPolicies, Sig> caller;
    v8::Local<v8::Value> c = caller::create(f, CallPolicies());
    v8::Local<v8::Function> function = v8::Function::New(v8::Isolate::GetCurrent(), caller::call, c);
    v8::Local<v8::Object> prototype = pt->GetFunction()->NewInstance();
    prototype->SetInternalField(0, c);
    v8::Local<v8::Value> original_proto = function->GetPrototype();
    function->SetPrototype(prototype);
    prototype->SetPrototype(original_proto);
    return function;
}
コード例 #7
0
ファイル: rtScriptNode.cpp プロジェクト: madanagopalt/pxCore
void rtNodeContext::clonedEnvironment(rtNodeContextRef clone_me)
{
  rtLogDebug(__FUNCTION__);
  Locker                locker(mIsolate);
  Isolate::Scope isolate_scope(mIsolate);
  HandleScope     handle_scope(mIsolate);

  // Get parent Local context...
  Local<Context> local_context = clone_me->getLocalContext();
  Context::Scope context_scope(local_context);

  // Create dummy sandbox for ContextifyContext::makeContext() ...
  Local<Object> sandbox = Object::New(mIsolate);

  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  if( clone_me->has(SANDBOX_IDENTIFIER) )
  {
    rtValue       val_array = clone_me->get(SANDBOX_IDENTIFIER);
    rtObjectRef       array = val_array.toObject();

    int len = array.get<int>("length");

    rtString s;
    for(int i = 0; i < len; i++)
    {
      array.get<rtString>( (uint32_t) i, s);  // get 'name' for object
      rtValue obj = clone_me->get(s);         // get object for 'name'

      if( obj.isEmpty() == false)
      {
          // Copy to var/module 'sandbox' under construction...
          Local<Value> module = local_context->Global()->Get( String::NewFromUtf8(mIsolate, s.cString() ) );
          sandbox->Set( String::NewFromUtf8(mIsolate, s.cString()), module);
      }
      else
      {
        rtLogError("## FATAL:   '%s' is empty !! - UNEXPECTED", s.cString());
      }
    }
  }
  else
  {
    rtLogWarn("## WARNING:   '%s' is undefined !! - UNEXPECTED", SANDBOX_IDENTIFIER);
  }
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  //
  // Clone a new context.
  {
    Local<Context>  clone_local = node::makeContext(mIsolate, sandbox); // contextify context with 'sandbox'

    clone_local->SetEmbedderData(HandleMap::kContextIdIndex, Integer::New(mIsolate, mId));
#ifdef ENABLE_NODE_V_6_9
    Local<Context> envCtx = Environment::GetCurrent(mIsolate)->context();
    Local<String> symbol_name = FIXED_ONE_BYTE_STRING(mIsolate, "_contextifyPrivate");
    Local<Private> private_symbol_name = Private::ForApi(mIsolate, symbol_name);
    MaybeLocal<Value> maybe_value = sandbox->GetPrivate(envCtx,private_symbol_name);
    Local<Value> decorated;
    if (true == maybe_value.ToLocal(&decorated))
    {
      mContextifyContext = decorated.As<External>()->Value();
    }
#else
    Local<String> hidden_name = FIXED_ONE_BYTE_STRING(mIsolate, "_contextifyHidden");
    mContextifyContext = sandbox->GetHiddenValue(hidden_name).As<External>()->Value();
#endif

    mContextId = GetContextId(clone_local);

    mContext.Reset(mIsolate, clone_local); // local to persistent
    // commenting below code as templates are isolcate specific
/*
    Context::Scope context_scope(clone_local);

    Handle<Object> clone_global = clone_local->Global();

    // Register wrappers in this cloned context...
      rtObjectWrapper::exportPrototype(mIsolate, clone_global);
    rtFunctionWrapper::exportPrototype(mIsolate, clone_global);

    mRtWrappers.Reset(mIsolate, clone_global);
*/
}

  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}
コード例 #8
0
ファイル: rtScriptNode.cpp プロジェクト: madanagopalt/pxCore
void rtNodeContext::createEnvironment()
{
  rtLogDebug(__FUNCTION__);
  Locker                locker(mIsolate);
  Isolate::Scope isolate_scope(mIsolate);
  HandleScope     handle_scope(mIsolate);

  // Create a new context.
  Local<Context> local_context = Context::New(mIsolate);

#ifdef ENABLE_NODE_V_6_9

  local_context->SetEmbedderData(HandleMap::kContextIdIndex, Integer::New(mIsolate, mId));

  mContextId = GetContextId(local_context);

  mContext.Reset(mIsolate, local_context); // local to persistent

  Context::Scope context_scope(local_context);

  Handle<Object> global = local_context->Global();



  mRtWrappers.Reset(mIsolate, global);

  // Create Environment.

#if NODE_VERSION_AT_LEAST(8,9,4)
  IsolateData *isolateData = new IsolateData(mIsolate,uv_default_loop(),array_buffer_allocator->zero_fill_field());

  mEnv = CreateEnvironment(isolateData,
#else
  mEnv = CreateEnvironment(mIsolate,
                           uv_default_loop(),
#endif
                           local_context,
#ifdef ENABLE_DEBUG_MODE
                           g_argc,
                           g_argv,
#else
                           s_gArgs->argc,
                           s_gArgs->argv,
#endif
                           exec_argc,
                           exec_argv);

#if !NODE_VERSION_AT_LEAST(8,9,4)
   array_buffer_allocator->set_env(mEnv);
#endif
  mIsolate->SetAbortOnUncaughtExceptionCallback(
        ShouldAbortOnUncaughtException);
#ifdef ENABLE_DEBUG_MODE
#if !NODE_VERSION_AT_LEAST(8,9,4)
  // Start debug agent when argv has --debug
  if (use_debug_agent)
  {
    rtLogWarn("use_debug_agent\n");
#ifdef HAVE_INSPECTOR
    if (use_inspector)
    {
      char currentPath[100];
      memset(currentPath,0,sizeof(currentPath));
      const char *rv = getcwd(currentPath,sizeof(currentPath));
      (void)rv;
      StartDebug(mEnv, currentPath, debug_wait_connect, mPlatform);
    }
    else
#endif
    {
      StartDebug(mEnv, NULL, debug_wait_connect);
    }
  }
#else
#if HAVE_INSPECTOR
//     if( !mEnv->inspector_agent()->IsStarted() )
//         mEnv->inspector_agent()->Start(mPlatform, nullptr, debug_options);
#endif
#endif
#endif
// Load Environment.
  {
    Environment::AsyncCallbackScope callback_scope(mEnv);
    LoadEnvironment(mEnv);
  }
#if defined(ENABLE_DEBUG_MODE) && !NODE_VERSION_AT_LEAST( 8, 9, 4 )
  if (use_debug_agent)
  {
    rtLogWarn("use_debug_agent\n");
    EnableDebug(mEnv);
  }
#endif
    rtObjectWrapper::exportPrototype(mIsolate, global);
    rtFunctionWrapper::exportPrototype(mIsolate, global);

    {
      SealHandleScope seal(mIsolate);
#ifndef RUNINMAIN
      EmitBeforeExit(mEnv);
#else
      bool more;
#ifdef ENABLE_NODE_V_6_9
      v8::platform::PumpMessageLoop(mPlatform, mIsolate);
#endif //ENABLE_NODE_V_6_9
      more = uv_run(mEnv->event_loop(), UV_RUN_ONCE);
      if (more == false)
      {
        EmitBeforeExit(mEnv);
      }
#endif

    }
#else
  local_context->SetEmbedderData(HandleMap::kContextIdIndex, Integer::New(mIsolate, mId));

  mContextId = GetContextId(local_context);

  mContext.Reset(mIsolate, local_context); // local to persistent

  Context::Scope context_scope(local_context);

  Handle<Object> global = local_context->Global();

  // Register wrappers.
  rtObjectWrapper::exportPrototype(mIsolate, global);
  rtFunctionWrapper::exportPrototype(mIsolate, global);

  mRtWrappers.Reset(mIsolate, global);

  // Create Environment.
  mEnv = CreateEnvironment(mIsolate,
                           uv_default_loop(),
                           local_context,
#ifdef ENABLE_DEBUG_MODE
                           g_argc,
                           g_argv,
#else
                           s_gArgs->argc,
                           s_gArgs->argv,
#endif
                           exec_argc,
                           exec_argv);

  // Start debug agent when argv has --debug
#ifdef ENABLE_DEBUG_MODE
  if (use_debug_agent)
  {
    rtLogWarn("use_debug_agent\n");
    StartDebug(mEnv, debug_wait_connect);
  }
#endif

  // Load Environment.
  LoadEnvironment(mEnv);

  // Enable debugger
  if (use_debug_agent)
  {
    EnableDebug(mEnv);
  }
#endif //ENABLE_NODE_V_6_9
}