Example #1
0
Handle<Value> EnvWrap::open(const Arguments& args) {
    HandleScope scope;
    int rc;
    int flags = 0;
    
    // Get the wrapper
    EnvWrap *ew = ObjectWrap::Unwrap<EnvWrap>(args.This());
    
    if (!ew->env) {
        ThrowException(Exception::Error(String::New("The environment is already closed.")));
        return Undefined();
    }
    
    Local<Object> options = args[0]->ToObject();
    Local<String> path = options->Get(String::NewSymbol("path"))->ToString();
    
    rc = applyUint32Setting(&mdb_env_set_maxdbs, ew->env, options, 1, "maxDbs");
    if (rc != 0) {
        ThrowException(Exception::Error(String::New(mdb_strerror(rc))));
        return Undefined();
    }
    
    rc = applyUint32Setting(&mdb_env_set_mapsize, ew->env, options, 10485760, "mapSize");
    if (rc != 0) {
        ThrowException(Exception::Error(String::New(mdb_strerror(rc))));
        return Undefined();
    }

    // TODO: expose mdb_env_set_maxreaders
    
    // NOTE: MDB_FIXEDMAP is not exposed here since it is "highly experimental" + it is irrelevant for this use case
    // NOTE: MDB_NOTLS is not exposed here because it is irrelevant for this use case, as node will run all this on a single thread anyway
    setFlagFromValue(&flags, MDB_NOSUBDIR, "noSubdir", false, options);
    setFlagFromValue(&flags, MDB_RDONLY, "readOnly", false, options);
    setFlagFromValue(&flags, MDB_WRITEMAP, "useWritemap", false, options);
    setFlagFromValue(&flags, MDB_NOMETASYNC, "noMetaSync", false, options);
    setFlagFromValue(&flags, MDB_NOSYNC, "noSync", false, options);
    setFlagFromValue(&flags, MDB_MAPASYNC, "mapAsync", false, options);
    
    int l = path->Length();
    char *cpath = new char[l + 1];
    path->WriteAscii(cpath);
    cpath[l] = 0;
    
    // TODO: make file attributes configurable
    rc = mdb_env_open(ew->env, cpath, flags, 0664);
    
    if (rc != 0) {
        mdb_env_close(ew->env);
        ew->env = NULL;
        ThrowException(Exception::Error(String::New(mdb_strerror(rc))));
        return Undefined();
    }
    
    return Undefined();
}
Example #2
0
// 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;
}
Example #3
0
File: V8.cpp Project: kenahoo/V8
// [[Rcpp::export]]
Rcpp::RawVector context_get_bin(std::string name, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx) {
  // Test if context still exists
  if(!ctx)
    throw std::runtime_error("Context has been disposed.");

  // Create scope
  HandleScope handle_scope;
  Context::Scope context_scope(*ctx);
  v8::Handle<v8::Object> global = (*ctx)->Global();

  //find the string
  Local<v8::String> mystring = global->Get(String::NewSymbol(name.c_str()))->ToString();
  Rcpp::RawVector res(mystring->Length());
  mystring->WriteAscii((char*) res.begin());
  return res;
}
Example #4
0
String::AsciiValue::AsciiValue(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->Length();
    // Need space for the NULL terminator.
    mStr = new char[len + 1];
    mLength = str->WriteAscii(mStr, 0, len + 1);
  }
}