static std::string toStdString(Local<String> s) { char *buf = new char[s->Length() + 1]; s->WriteUtf8(buf); std::string result(buf); delete[] buf; return result; }
void StringIMPL::Value(char *target) const { Isolate::Scope isolate_scope(g_CurrentVM); HandleScope handle_scope(g_CurrentVM); Local<Context> context = v8::Local<v8::Context>::New(g_CurrentVM, g_GlobalContext); Context::Scope context_scope(context); Local<v8::String> stringValue = Handle<v8::String>::New(g_CurrentVM, persisted_value); stringValue->WriteUtf8(target); }
char* V8StringToFunctionChar(Handle<String> str) { Local<String> prefix = String::New("("); Local<String> postfix = String::New(")();"); Local<String> result = String::Concat(prefix,str); result = String::Concat(result,postfix); int len = result->Utf8Length(); char* buf = new char[len + 1]; result->WriteUtf8(buf, len); buf[len] = '\0'; return buf; }
// Returns number of bytes written. ssize_t DecodeWrite(char *buf, size_t buflen, Handle<Value> val, enum encoding encoding) { HandleScope scope; // XXX // A lot of improvement can be made here. See: // http://code.google.com/p/v8/issues/detail?id=270 // http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611 if (val->IsArray()) { fprintf(stderr, "'raw' encoding (array of integers) has been removed. " "Use 'binary'.\n"); assert(0); return -1; } Local<String> str = val->ToString(); if (encoding == UTF8) { str->WriteUtf8(buf, buflen, NULL, String::HINT_MANY_WRITES_EXPECTED); return buflen; } if (encoding == ASCII) { str->WriteAscii(buf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED); return buflen; } // THIS IS AWFUL!!! FIXME assert(encoding == BINARY); uint16_t * twobytebuf = new uint16_t[buflen]; str->Write(twobytebuf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED); for (size_t i = 0; i < buflen; i++) { unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]); buf[i] = b[0]; } delete [] twobytebuf; return buflen; }
String::Utf8Value::Utf8Value(Handle<v8::Value> val) { // Set some defaults which will be used for empty values/strings mStr = NULL; mLength = 0; if (val.IsEmpty()) { return; } // TODO: Do we need something about HandleScope here? Local<String> str = val->ToString(); if (!str.IsEmpty()) { int len = str->Utf8Length(); // Need space for the NULL terminator. mStr = new char[len + 1]; mLength = str->WriteUtf8(mStr, len + 1) - 1; } }
// static void Query::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall()) { if (args.Length() != 1) { Local<String> str = String::NewFromUtf8(isolate, "Consturctor expects 1 agument"); isolate->ThrowException(Exception::TypeError(str)); return; } if (!args[0]->IsString()) { Local<String> str = String::NewFromUtf8(isolate, "Argument must be a string"); isolate->ThrowException(Exception::TypeError(str)); return; } Local<String> str = args[0]->ToString(); QByteArray arr; arr.resize(str->Utf8Length()); str->WriteUtf8(arr.data(), arr.length()); QString queryString = QString::fromUtf8(arr); Query* query = new Query(queryString); query->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function, turn into construct call. const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, s_constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
Handle<Value> CharsetMap_recode_in(const Arguments &args) { HandleScope scope; DEBUG_MARKER(UDEB_DEBUG); REQUIRE_ARGS_LENGTH(5); JsValueConverter<CharsetMap *> converter(args.Holder()); CharsetMap * csmap = converter.toC(); int32_t lengths[2]; enum { SOURCE = 0 , DEST = 1 }; // for lengths[] int status = CharsetMap::RECODE_OK; int copyLen; Local<Object> NodeBuffer = args[2]->ToObject(); Local<String> sourceStr = args[0]->ToString(); int32_t cs_to = args[1]->Int32Value(); char * buffer = node::Buffer::Data(NodeBuffer); uint32_t offset = args[3]->Uint32Value(); lengths[DEST] = args[4]->Int32Value(); /* Source string length and charset */ int32_t cs_from = csmap->getUTF16CharsetNumber(); lengths[SOURCE] = sourceStr->Length(); /* Special case: if the destination is 2-byte unicode, just copy directly. sourceStr->Write(uint16_t * ...) might face alignment issues, so we use memcpy(). */ if(cs_to == cs_from) { if(lengths[DEST] >= lengths[SOURCE]) { copyLen = lengths[SOURCE]; } else { copyLen = lengths[DEST]; status = csmap->RECODE_BUFF_TOO_SMALL; } DEBUG_PRINT("recodeIn() optimized path UTF16 -> UTF16 using memcpy"); String::Value source(sourceStr); memcpy(buffer + offset, *source, copyLen); lengths[DEST] = copyLen; } /* Special case: if the destination is UTF-8, let V8 do the copying. copyLen will receive the length written in characters. The return value is the length written in bytes. */ else if(cs_to == csmap->getUTF8CharsetNumber()) { lengths[DEST] = sourceStr->WriteUtf8(buffer + offset, lengths[DEST], ©Len, 1); if(copyLen < sourceStr->Length()) { status = CharsetMap::RECODE_BUFF_TOO_SMALL; } DEBUG_PRINT("recodeIn() UTF16 -> UTF8 using v8 WriteUtf8(): %s", buffer + offset); } /* General case: use CharsetMap::recode() */ else { String::Value source(sourceStr); status = csmap->recode(lengths, cs_from, cs_to, *source, buffer + offset); DEBUG_PRINT("recodeIn() UTF16 -> X using recode(%c%c%c%c...): %s", (*source)[1], (*source)[3], (*source)[5], (*source)[7], buffer + offset); } /* Build the return value */ Local<Object> returnVal = Object::New(); returnVal->Set(String::NewSymbol("status"), Integer::New(status)); returnVal->Set(String::NewSymbol("lengthIn"), Integer::New(lengths[SOURCE])); returnVal->Set(String::NewSymbol("lengthOut"), Integer::New(lengths[DEST])); returnVal->Set(String::NewSymbol("charset"), args[1]); returnVal->Set(String::NewSymbol("offset"), Integer::New(offset)); return scope.Close(returnVal); }
Handle<Value> Pngquant::Compress(const Arguments& args) { HandleScope scope; struct rwpng_data * out_buffer; struct rwpng_data * in_buffer; if (args.Length() != 3) { return ThrowException(Exception::TypeError( String::New("Invalid argument, Need three arguments!") ) ); } if (!Buffer::HasInstance(args[0])) { return ThrowException(Exception::TypeError( String::New("First argument must be a buffer.") ) ); } if (!args[2]->IsFunction()) { return ThrowException(Exception::TypeError( String::New("Third argument must be a callback function.") ) ); } png_bytep in_stream = (png_bytep) Buffer::Data(args[0]->ToObject()); unsigned in_length = Buffer::Length(args[0]->ToObject()); Local<String> opt = args[1]->ToString(); Local<Function> callback = Local<Function>::Cast(args[2]); Buffer *buffer; char str_buffer[BUFFER_SIZE]; memset(str_buffer, '\0', BUFFER_SIZE); opt->WriteUtf8(str_buffer); char *argv[32] = {""}; char token[BUFFER_SIZE]; memset(token, '\0', BUFFER_SIZE); int i = 0, argc = 0, k = 0, len = 0; while(str_buffer[i] != '\0') { if (argc >= 30) { break; } if (str_buffer[i] == ' ') { argc++; k = 0; len = strlen(token); argv[argc] = (char*) malloc(len + 1); memset(argv[argc], '\0', len + 1); memcpy(argv[argc], token, len + 1); //reset token memset(token, '\0', BUFFER_SIZE); } else { token[k++] = str_buffer[i]; } i++; } argv[0] = "pngquant"; if ((len = strlen(token)) > 0) { argc++; argv[argc] = (char*) malloc(len + 1); memset(argv[argc], '\0', len + 1); memcpy(argv[argc], token, len + 1); argc = argc + 1; //0 1 2 } in_buffer = (struct rwpng_data *)malloc(sizeof(struct rwpng_data)); if (in_buffer == NULL) { return ThrowException(Exception::TypeError( String::New("malloc fail!") ) ); } out_buffer = (struct rwpng_data *)malloc(sizeof(struct rwpng_data)); if (out_buffer == NULL) { return ThrowException(Exception::TypeError( String::New("malloc fail!") ) ); } memset(out_buffer, '\0', sizeof(struct rwpng_data)); in_buffer->png_data = in_stream; in_buffer->length = in_length; in_buffer->bytes_read = 0; int ret = pngquant(in_buffer, out_buffer, argc, argv); if (ret != 0) { out_buffer->png_data = in_buffer->png_data; out_buffer->length = in_buffer->length; fprintf(stderr, "File: %s\n", argv[argc-1]); } buffer = Buffer::New((char *)out_buffer->png_data, out_buffer->length); free(in_buffer); free(out_buffer); return scope.Close( buffer->handle_ ); }
Parameter* ODBC::GetParametersFromArray (Local<Array> values, int *paramCount) { DEBUG_PRINTF("ODBC::GetParametersFromArray\n"); *paramCount = values->Length(); Parameter* params = (Parameter *) malloc(*paramCount * sizeof(Parameter)); for (int i = 0; i < *paramCount; i++) { Local<Value> value = values->Get(i); params[i].size = 0; params[i].length = SQL_NULL_DATA; params[i].buffer_length = 0; params[i].decimals = 0; DEBUG_PRINTF("ODBC::GetParametersFromArray - ¶m[%i].length = %X\n", i, ¶ms[i].length); if (value->IsString()) { Local<String> string = value->ToString(); int length = string->Length(); params[i].c_type = SQL_C_TCHAR; #ifdef UNICODE params[i].type = (length >= 8000) ? SQL_WLONGVARCHAR : SQL_WVARCHAR; params[i].buffer_length = (length * sizeof(uint16_t)) + sizeof(uint16_t); #else params[i].type = (length >= 8000) ? SQL_LONGVARCHAR : SQL_VARCHAR; params[i].buffer_length = string->Utf8Length() + 1; #endif params[i].buffer = malloc(params[i].buffer_length); params[i].size = params[i].buffer_length; params[i].length = SQL_NTS;//params[i].buffer_length; #ifdef UNICODE string->Write((uint16_t *) params[i].buffer); #else string->WriteUtf8((char *) params[i].buffer); #endif DEBUG_PRINTF("ODBC::GetParametersFromArray - IsString(): params[%i] " "c_type=%i type=%i buffer_length=%i size=%i length=%i " "value=%s\n", i, params[i].c_type, params[i].type, params[i].buffer_length, params[i].size, params[i].length, (char*) params[i].buffer); } else if (value->IsNull()) { params[i].c_type = SQL_C_DEFAULT; params[i].type = SQL_VARCHAR; params[i].length = SQL_NULL_DATA; DEBUG_PRINTF("ODBC::GetParametersFromArray - IsNull(): params[%i] " "c_type=%i type=%i buffer_length=%i size=%i length=%i\n", i, params[i].c_type, params[i].type, params[i].buffer_length, params[i].size, params[i].length); } else if (value->IsInt32()) { int64_t *number = new int64_t(value->IntegerValue()); params[i].c_type = SQL_C_SBIGINT; params[i].type = SQL_BIGINT; params[i].buffer = number; params[i].length = 0; DEBUG_PRINTF("ODBC::GetParametersFromArray - IsInt32(): params[%i] " "c_type=%i type=%i buffer_length=%i size=%i length=%i " "value=%lld\n", i, params[i].c_type, params[i].type, params[i].buffer_length, params[i].size, params[i].length, *number); } else if (value->IsNumber()) { double *number = new double(value->NumberValue()); params[i].c_type = SQL_C_DOUBLE; params[i].type = SQL_DECIMAL; params[i].buffer = number; params[i].buffer_length = sizeof(double); params[i].length = params[i].buffer_length; params[i].decimals = 7; params[i].size = sizeof(double); DEBUG_PRINTF("ODBC::GetParametersFromArray - IsNumber(): params[%i] " "c_type=%i type=%i buffer_length=%i size=%i length=%i " "value=%f\n", i, params[i].c_type, params[i].type, params[i].buffer_length, params[i].size, params[i].length, *number); } else if (value->IsBoolean()) { bool *boolean = new bool(value->BooleanValue()); params[i].c_type = SQL_C_BIT; params[i].type = SQL_BIT; params[i].buffer = boolean; params[i].length = 0; DEBUG_PRINTF("ODBC::GetParametersFromArray - IsBoolean(): params[%i] " "c_type=%i type=%i buffer_length=%i size=%i length=%i\n", i, params[i].c_type, params[i].type, params[i].buffer_length, params[i].size, params[i].length); } } return params; }
Parameter* ODBC::GetParametersFromArray (Local<Array> values, int *paramCount) { DEBUG_PRINTF("ODBC::GetParametersFromArray\n"); *paramCount = values->Length(); Parameter* params = NULL; if (*paramCount > 0) { params = (Parameter *) malloc(*paramCount * sizeof(Parameter)); } for (int i = 0; i < *paramCount; i++) { Local<Value> value = values->Get(i); params[i].ColumnSize = 0; params[i].StrLen_or_IndPtr = SQL_NULL_DATA; params[i].BufferLength = 0; params[i].DecimalDigits = 0; DEBUG_PRINTF("ODBC::GetParametersFromArray - param[%i].length = %lli\n", i, params[i].StrLen_or_IndPtr); if (value->IsString()) { Local<String> string = value->ToString(); int length = string->Length(); params[i].ValueType = SQL_C_TCHAR; params[i].ColumnSize = 0; //SQL_SS_LENGTH_UNLIMITED #ifdef UNICODE params[i].ParameterType = SQL_WVARCHAR; params[i].BufferLength = (length * sizeof(uint16_t)) + sizeof(uint16_t); #else params[i].ParameterType = SQL_VARCHAR; params[i].BufferLength = string->Utf8Length() + 1; #endif params[i].ParameterValuePtr = malloc(params[i].BufferLength); params[i].StrLen_or_IndPtr = SQL_NTS;//params[i].BufferLength; #ifdef UNICODE string->Write((uint16_t *) params[i].ParameterValuePtr); #else string->WriteUtf8((char *) params[i].ParameterValuePtr); #endif DEBUG_PRINTF("ODBC::GetParametersFromArray - IsString(): params[%i] c_type=%i type=%i buffer_length=%lli size=%lli length=%lli value=%s\n", i, params[i].ValueType, params[i].ParameterType, params[i].BufferLength, params[i].ColumnSize, params[i].StrLen_or_IndPtr, (char*) params[i].ParameterValuePtr); } else if (value->IsNull()) { params[i].ValueType = SQL_C_DEFAULT; params[i].ParameterType = SQL_VARCHAR; params[i].StrLen_or_IndPtr = SQL_NULL_DATA; DEBUG_PRINTF("ODBC::GetParametersFromArray - IsNull(): params[%i] c_type=%i type=%i buffer_length=%lli size=%lli length=%lli\n", i, params[i].ValueType, params[i].ParameterType, params[i].BufferLength, params[i].ColumnSize, params[i].StrLen_or_IndPtr); } else if (value->IsInt32()) { int64_t *number = new int64_t(value->IntegerValue()); params[i].ValueType = SQL_C_SBIGINT; params[i].ParameterType = SQL_BIGINT; params[i].ParameterValuePtr = number; params[i].StrLen_or_IndPtr = 0; DEBUG_PRINTF("ODBC::GetParametersFromArray - IsInt32(): params[%i] c_type=%i type=%i buffer_length=%lli size=%lli length=%lli value=%lld\n", i, params[i].ValueType, params[i].ParameterType, params[i].BufferLength, params[i].ColumnSize, params[i].StrLen_or_IndPtr, *number); } else if (value->IsNumber()) { double *number = new double(value->NumberValue()); params[i].ValueType = SQL_C_DOUBLE; params[i].ParameterType = SQL_DECIMAL; params[i].ParameterValuePtr = number; params[i].BufferLength = sizeof(double); params[i].StrLen_or_IndPtr = params[i].BufferLength; params[i].DecimalDigits = 7; params[i].ColumnSize = sizeof(double); DEBUG_PRINTF("ODBC::GetParametersFromArray - IsNumber(): params[%i] c_type=%i type=%i buffer_length=%lli size=%lli length=%lli value=%f\n", i, params[i].ValueType, params[i].ParameterType, params[i].BufferLength, params[i].ColumnSize, params[i].StrLen_or_IndPtr, *number); } else if (value->IsBoolean()) { bool *boolean = new bool(value->BooleanValue()); params[i].ValueType = SQL_C_BIT; params[i].ParameterType = SQL_BIT; params[i].ParameterValuePtr = boolean; params[i].StrLen_or_IndPtr = 0; DEBUG_PRINTF("ODBC::GetParametersFromArray - IsBoolean(): params[%i] c_type=%i type=%i buffer_length=%lli size=%lli length=%lli\n", i, params[i].ValueType, params[i].ParameterType, params[i].BufferLength, params[i].ColumnSize, params[i].StrLen_or_IndPtr); } } return params; }
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(); }