Ejemplo n.º 1
0
function flusspferd::create_function(
    std::string const &name,
    unsigned n_args,
    std::vector<std::string> argnames,
    flusspferd::string const &body,
    std::string const &file,
    unsigned line)
{
  JSContext *cx = Impl::current_context();

  std::vector<char const *> argnames_c;
  argnames_c.reserve(argnames.size());

  for (std::vector<std::string>::const_iterator it = argnames.begin();
      it != argnames.end();
      ++it)
    argnames_c.push_back(it->c_str());

  JSFunction *fun =
      JS_CompileUCFunction(
        cx,
        0,
        name.c_str(),
        n_args,
        &argnames_c[0],
        body.data(),
        body.length(),
        file.c_str(),
        line);

  if (!fun)
    throw exception("Could not compile function");

  return Impl::wrap_function(fun);
}
Ejemplo n.º 2
0
object sqlite3::compile(flusspferd::string sql_in, value bind ) {
    local_root_scope scope;

    size_t n_bytes = sql_in.length() * 2;
    sqlite3_stmt * sth = 0;
    js_char16_t * tail = 0; // uncompiled part of the sql (when multiple stmts)
    
    if (sqlite3_prepare16_v2(db, sql_in.data(), n_bytes, &sth, (const void**)&tail) != SQLITE_OK)
    {
        raise_sqlite_error(db);
    }

    object cursor = create<sqlite3_cursor>(fusion::make_vector(sth));

    string tail_str;
    if (tail) {
        tail_str = string(tail);
    }
    
    string sql = sql_in.substr( 0, sql_in.size() - tail_str.size() );

    cursor.define_property("sql", sql);
    cursor.define_property("tail", tail_str);        

    if ( !bind.is_undefined_or_null() ) {
        cursor.call("bind", bind );
    }
    
    return cursor;
}