Esempio n. 1
0
bool CQueryParameter::TransformParameter( Isolate* isolate, Local< Value > value, sBindParameter** pOutParam )
{
	HandleScope scope( isolate );
	Local< Context > context = isolate->GetCurrentContext( );

	sBindParameter* pParameter = *pOutParam;

	if( value->IsNull( ) )
	{
		pParameter->SetNull( );
	}
	else if( value->IsString( ) )
	{
		pParameter->SetString( value.As<String>( ) );
	}
	else if( value->IsBoolean( ) )
	{
		pParameter->SetBool( value->BooleanValue( context ).FromJust( ) );
	}
	else if( value->IsInt32( ) )
	{
		pParameter->SetInt( value->Int32Value( context ).FromJust( ) );
	}
	else if( value->IsUint32( ) )
	{
		pParameter->SetUInt( value->Uint32Value( context ).FromJust( ) );
	}
	else if( value->IsNumber( ) )
	{
		double d = value->NumberValue( context ).FromJust( );
		if( _isnan( d ) || !_finite( d ) )
		{
			return false;
		}
		else if( d == floor( d ) &&
				 d >= std::numeric_limits<int64_t>::min( ) &&
				 d <= std::numeric_limits<int64_t>::max( )
				 )
		{
			pParameter->SetInt64( value->IntegerValue( context ).FromJust( ) );
		}
		else
		{
			pParameter->SetDouble( value->NumberValue( context ).FromJust( ) );
		}
	}
	else if( value->IsDate( ) )
	{
		Local<Date> dateObject = value.As<Date>( );

		pParameter->SetDate( dateObject->NumberValue( context ).FromJust( ) );
	}
	else if( value->IsObject( ) )
	{
		Local<Object> objValue = value.As<Object>( );

		if( node::Buffer::HasInstance( value ) )
		{
			pParameter->SetBuffer( isolate, objValue );
		}
		else
		{
			Local< String > strId = String::NewFromUtf8( isolate, "_objId", NewStringType::kNormal ).ToLocalChecked( );


			if( objValue->HasRealNamedProperty( context, strId ).FromJust( ) )
			{
				Local< Uint32 > numTypeId = objValue->Get( context, strId ).ToLocalChecked( ).As< Uint32 >( );

				uint32_t nType = numTypeId->Int32Value( context ).FromJust( );

				switch( nType )
				{
					case ID_INPUT_STREAM:
					{
						Local<Value> type = objValue->Get(
							context, String::NewFromUtf8( isolate, "type", NewStringType::kNormal ).ToLocalChecked( )
						).ToLocalChecked( );

						Local<Value> stream = objValue->Get(
							context, String::NewFromUtf8( isolate, "stream", NewStringType::kNormal ).ToLocalChecked( )
						).ToLocalChecked( );

						Local<Value> length = objValue->Get(
							context, String::NewFromUtf8( isolate, "length", NewStringType::kNormal ).ToLocalChecked( )
						).ToLocalChecked( );

						pParameter->SetStream( isolate, type->Uint32Value( context ).FromJust( ), stream, static_cast< SQLUINTEGER >( length->IntegerValue( context ).FromJust( ) ) );

						break;
					}
					case ID_NUMERIC_VALUE:
					{
						Local< Value > precision = objValue->Get(
							context, String::NewFromUtf8( isolate, "precision", NewStringType::kNormal ).ToLocalChecked( )
						).ToLocalChecked( );

						Local<Value> scale = objValue->Get(
							context, String::NewFromUtf8( isolate, "scale", NewStringType::kNormal ).ToLocalChecked( )
						).ToLocalChecked( );

						Local<Value> sign = objValue->Get(
							context, String::NewFromUtf8( isolate, "sign", NewStringType::kNormal ).ToLocalChecked( )
						).ToLocalChecked( );

						Local<Value> value = objValue->Get(
							context, String::NewFromUtf8( isolate, "value", NewStringType::kNormal ).ToLocalChecked( )
						).ToLocalChecked( );

						Local< Uint32 > numPrecision = precision.As<Uint32>( );
						Local< Uint32 > numScale = scale.As<Uint32>( );
						Local< Boolean > boolSign = sign.As<Boolean>( );
						Local< ArrayBuffer > arrBuffer = ( value.As<Uint8Array>( ) )->Buffer( );

						ArrayBuffer::Contents contents = arrBuffer->GetContents( );

						if( contents.ByteLength( ) > SQL_MAX_NUMERIC_LEN )
						{
							return false;
						}

						pParameter->SetNumeric( numPrecision->Uint32Value( context ).FromJust( ),
												numScale->Uint32Value( context ).FromJust( ),
												boolSign->BooleanValue( context ).FromJust( ),
												contents.Data( ),
												contents.ByteLength( )
						);


						break;
					}
					default:
					{
						return false;
					}
				}
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}

	return true;
}
Handle<Value>
NodeSandbox::node_spawn(const Arguments& args)
{
  HandleScope scope;
  char** argv;
  std::map<std::string, std::string> envp;
  SandboxWrapper* wrap;

  wrap = node::ObjectWrap::Unwrap<SandboxWrapper>(args.This());
  argv = static_cast<char**>(calloc (sizeof (char*), args.Length()+1));
  argv[args.Length()] = nullptr;

  for(int i = 0; i < args.Length(); i++) {
    if (args[i]->IsString()) {
      Local<String> v = args[i]->ToString();
      argv[i] = static_cast<char*>(calloc(sizeof(char), v->Utf8Length()+1));
      v->WriteUtf8(argv[i]);
    } else {
      if (i <= args.Length() - 2 ) {
        ThrowException(Exception::TypeError(String::New("Arguments must be strings.")));
        goto out;
      } else {
        // Last parameter is an options structure
        Local<Object> options = args[i]->ToObject();
        if (!options.IsEmpty()) {
          if (options->HasRealNamedProperty(String::NewSymbol("env"))) {
            Local<Object> envOptions = options->Get(String::NewSymbol("env"))->ToObject();
            if (!envOptions.IsEmpty()) {
              Local<Array> envArray = envOptions->GetOwnPropertyNames();
              for (uint32_t i = 0; i < envArray->Length(); i++) {
                std::vector<char> strName;
                std::vector<char> strValue;
                Local<String> envName;
                Local<String> envValue;

                if (!(envArray->Get(i)->IsString() && envArray->Get(i)->IsString()))
                  goto err_env;

                envName = envArray->Get(i)->ToString();
                envValue = envOptions->Get(envName)->ToString();

                strName.resize (envName->Utf8Length()+1);
                strValue.resize (envValue->Utf8Length()+1);
                envName->WriteUtf8 (strName.data());
                envValue->WriteUtf8 (strValue.data());
                envp.insert (std::make_pair(std::string (strName.data()), std::string(strValue.data())));
              }
            } else {
              goto err_env;
            }
          }
        } else {
          goto err_options;
        }
      }
    }
  }

  wrap->sbox->getVFS().setCWD ("/contract/");
  wrap->sbox->spawn(argv, envp);

  goto out;

err_env:
  ThrowException(Exception::TypeError(String::New("'env' option must be a map of string:string")));
  goto out;

err_options:
  ThrowException(Exception::TypeError(String::New("Last argument must be an options structure.")));
  goto out;

out:
  for (int i = 0; i < args.Length();i ++) {
    free (argv[i]);
  }
  free (argv);

  return Undefined();
}
Esempio n. 3
0
void Proxy::proxyConstructor(const v8::FunctionCallbackInfo<v8::Value>& args)
{
    LOGD(TAG, "proxy constructor callback!");
    Isolate* isolate = args.GetIsolate();
    EscapableHandleScope scope(isolate);

    JNIEnv *env = JNIScope::getEnv();
    Local<Object> jsProxy = args.This();

    // First things first, we need to wrap the object in case future calls need to unwrap proxy!
    Proxy* proxy = new Proxy(NULL);
    proxy->wrap(isolate, jsProxy);

    // every instance gets a special "_properties" object for us to use internally for get/setProperty
    jsProxy->DefineOwnProperty(isolate->GetCurrentContext(), propertiesSymbol.Get(isolate), Object::New(isolate), static_cast<PropertyAttribute>(DontEnum));

    // Now we hook up a java Object from the JVM...
    jobject javaProxy = ProxyFactory::unwrapJavaProxy(args); // do we already have one that got passed in?
    bool deleteRef = false;
    if (!javaProxy) {
        // No passed in java object, so let's create an instance
        // Look up java class from prototype...
        Local<Object> prototype = jsProxy->GetPrototype()->ToObject(isolate);
        Local<Function> constructor = prototype->Get(constructorSymbol.Get(isolate)).As<Function>();
        Local<External> wrap = constructor->Get(javaClassSymbol.Get(isolate)).As<External>();
        jclass javaClass = static_cast<jclass>(wrap->Value());

        // Now we create an instance of the class and hook it up
        JNIUtil::logClassName("Creating java proxy for class %s", javaClass);
        javaProxy = ProxyFactory::createJavaProxy(javaClass, jsProxy, args);
        deleteRef = true;
    }
    proxy->attach(javaProxy);

    int length = args.Length();

    if (length > 0 && args[0]->IsObject()) {
        bool extend = true;
        Local<Object> createProperties = args[0].As<Object>();
        Local<String> constructorName = createProperties->GetConstructorName();
        if (strcmp(*titanium::Utf8Value(constructorName), "Arguments") == 0) {
            extend = false;
            int32_t argsLength = createProperties->Get(STRING_NEW(isolate, "length"))->Int32Value();
            if (argsLength > 1) {
                Local<Value> properties = createProperties->Get(1);
                if (properties->IsObject()) {
                    extend = true;
                    createProperties = properties.As<Object>();
                }
            }
        }

        if (extend) {
            Local<Array> names = createProperties->GetOwnPropertyNames();
            int length = names->Length();
            Local<Object> properties = jsProxy->Get(propertiesSymbol.Get(isolate))->ToObject(isolate);

            for (int i = 0; i < length; ++i) {
                Local<Value> name = names->Get(i);
                Local<Value> value = createProperties->Get(name);
                bool isProperty = true;
                if (name->IsString()) {
                    Local<String> nameString = name.As<String>();
                    if (!jsProxy->HasRealNamedCallbackProperty(nameString)
                            && !jsProxy->HasRealNamedProperty(nameString)) {
                        jsProxy->Set(name, value);
                        isProperty = false;
                    }
                }
                if (isProperty) {
                    properties->Set(name, value);
                }
            }
        }
    }


    if (!args.Data().IsEmpty() && args.Data()->IsFunction()) {
        Local<Function> proxyFn = args.Data().As<Function>();
        Local<Value> *fnArgs = new Local<Value>[length];
        for (int i = 0; i < length; ++i) {
            fnArgs[i] = args[i];
        }
        proxyFn->Call(isolate->GetCurrentContext(), jsProxy, length, fnArgs);
    }

    if (deleteRef) {
        JNIEnv *env = JNIScope::getEnv();
        if (env) {
            env->DeleteLocalRef(javaProxy);
        }
    }

    args.GetReturnValue().Set(scope.Escape(jsProxy));
}
Esempio n. 4
0
Handle<Value> Proxy::proxyConstructor(const Arguments& args)
{
	HandleScope scope;
	JNIEnv *env = JNIScope::getEnv();
	Local<Object> jsProxy = args.Holder();

	Handle<Object> properties = Object::New();
	jsProxy->Set(propertiesSymbol, properties, PropertyAttribute(DontEnum));

	Handle<Object> prototype = jsProxy->GetPrototype()->ToObject();

	Handle<Function> constructor = Handle<Function>::Cast(prototype->Get(constructorSymbol));
	jclass javaClass = (jclass) External::Unwrap(constructor->Get(javaClassSymbol));

	// If ProxyFactory::createV8Proxy invoked us, unwrap
	// the pre-created Java proxy it sent.
	jobject javaProxy = ProxyFactory::unwrapJavaProxy(args);
	bool deleteRef = false;
	if (!javaProxy) {
		javaProxy = ProxyFactory::createJavaProxy(javaClass, jsProxy, args);
		deleteRef = true;
	}

	JNIUtil::logClassName("Create proxy: %s", javaClass);

	Proxy *proxy = new Proxy(javaProxy);
	proxy->Wrap(jsProxy);

	int length = args.Length();

	if (length > 0 && args[0]->IsObject()) {
		/*
		Handle<Value> argsStr = V8Util::jsonStringify(args[0]);
		String::Utf8Value str(argsStr);
		LOGV(TAG, "    with args: %s", *str);
		*/

		bool extend = true;
		Handle<Object> createProperties = args[0]->ToObject();
		Local<String> constructorName = createProperties->GetConstructorName();
		if (strcmp(*String::Utf8Value(constructorName), "Arguments") == 0) {
			extend = false;
			int32_t argsLength = createProperties->Get(String::New("length"))->Int32Value();
			if (argsLength > 1) {
				Handle<Value> properties = createProperties->Get(1);
				if (properties->IsObject()) {
					extend = true;
					createProperties = properties->ToObject();
				}
			}
		}

		if (extend) {
			Handle<Array> names = createProperties->GetOwnPropertyNames();
			int length = names->Length();

			for (int i = 0; i < length; ++i) {
				Handle<Value> name = names->Get(i);
				Handle<Value> value = createProperties->Get(name);
				bool isProperty = true;
				if (name->IsString()) {
					Handle<String> nameString = name->ToString();
					if (!jsProxy->HasRealNamedCallbackProperty(nameString)
						&& !jsProxy->HasRealNamedProperty(nameString)) {
						jsProxy->Set(name, value);
						isProperty = false;
					}
				}
				if (isProperty) {
					properties->Set(name, value);
				}
			}
		}
	}


	if (!args.Data().IsEmpty() && args.Data()->IsFunction()) {
		Handle<Function> proxyFn = Handle<Function>::Cast(args.Data());
		Handle<Value> *fnArgs = new Handle<Value>[length];
		for (int i = 0; i < length; ++i) {
			fnArgs[i] = args[i];
		}
		proxyFn->Call(jsProxy, length, fnArgs);
	}

	if (deleteRef) {
		JNIEnv *env = JNIScope::getEnv();
		if (env) {
			env->DeleteLocalRef(javaProxy);
		}
	}

	return jsProxy;
}