Beispiel #1
0
void
test_obj_defprop() {
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();

  Context::Scope context_scope(context);

  Handle<Object> obj = Object::New();
  Handle<Value> data = Integer::New(2);
  obj->SetAccessor(String::New("myprop"), ReadTestVal, WriteTestVal, data);
  Local<Object> global = context->Global();
  global->Set(String::New("testobj"), obj);

  Handle<String> source = String::New("var n = testobj.myprop; testobj.myprop = (n+9); testobj.myprop");

  // Compile the source code.
  Handle<Script> script = Script::Compile(source);

  // Run the script to get the result.
  Handle<Value> result = script->Run();

  do_check_true(!result.IsEmpty());
  do_check_true(result->IsInt32());
  JSInt32 i = result->Int32Value();
  do_check_eq(12, i);
  context.Dispose();
}
void
test_step_locked_does_not_block_main_thread()
{
  nsCOMPtr<mozIStorageConnection> db(getDatabase());

  // Need to prepare our statement ahead of time so we make sure to only test
  // step and not prepare.
  nsCOMPtr<mozIStorageStatement> stmt;
  nsresult rv = db->CreateStatement(NS_LITERAL_CSTRING(
    "INSERT INTO test (data) VALUES ('test1')"
  ), getter_AddRefs(stmt));
  do_check_success(rv);

  RefPtr<DatabaseLocker> locker(new DatabaseLocker("SELECT * FROM test"));
  do_check_true(locker);
  mozilla::ReentrantMonitorAutoEnter lock(locker->monitor);
  locker->RunInBackground();

  // Wait for the locker to notify us that it has locked the database properly.
  locker->WaitFor(WRITE_LOCK);

  bool hasResult;
  rv = stmt->ExecuteStep(&hasResult);
  do_check_eq(rv, NS_ERROR_FILE_IS_LOCKED);

  locker->Notify(TEST_DONE);
}
Beispiel #3
0
Handle<Value> AddOne(const Arguments& args) {
  do_check_eq(args.Length(), 1);
  Local<Value> v = args[0];
  do_check_true(v->IsNumber());
  Local<Number> n = v->ToNumber();
  return Number::New(n->Value() + 1.0);
}
Beispiel #4
0
void
test_Exception()
{
  HandleScope handle_scope;

  Persistent<Context> context = Context::New();
  Handle<Script> script = Script::New(String::New("function foo(x) { throw x; };"));

  Context::Scope scope(context);
  TryCatch trycatch;

  Handle<Value> v = script->Run();
  do_check_true(!v.IsEmpty());
  do_check_true(!trycatch.HasCaught());
  Handle<Function> fn = context->Global()->Get(String::NewSymbol("foo")).As<Function>();
  do_check_true(!fn.IsEmpty());
  Local<Value> args[1] = { Integer::New(4) };
  v = fn->Call(context->Global(), 1, args);
  do_check_true(v.IsEmpty());
  do_check_true(trycatch.HasCaught());
  Handle<Value> exn = trycatch.Exception();
  do_check_true(exn->IsInt32());
  do_check_eq(exn->Int32Value(), 4);

  context.Dispose();
}
Beispiel #5
0
void
test_Utf8Length()
{
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  char TEST_STRING[] = "this is a UTF-8 test!  This is pi: π";
  int TEST_LENGTH = strlen(TEST_STRING);
  Handle<String> str = String::New(TEST_STRING);
  do_check_eq(str->Utf8Length(), TEST_LENGTH);

  // Now, make a string with an embedded NULL.
  TEST_STRING[8] = '\0';
  str = String::New(TEST_STRING, TEST_LENGTH);
  do_check_eq(str->Utf8Length(), TEST_LENGTH);
  context.Dispose();
}
Beispiel #6
0
void
test_WriteAsciiEmpty() {
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  Handle<String> str = String::Empty();
  int written = str->WriteAscii(NULL, 0, 0);
  do_check_eq(written, 0);
  context.Dispose();
}
NS_IMETHODIMP
Spinner::HandleResult(mozIStorageResultSet *aResultSet)
{
  nsCOMPtr<mozIStorageRow> row;
  do_check_true(NS_SUCCEEDED(aResultSet->GetNextRow(getter_AddRefs(row))) && row);

  do_check_eq(row->AsInt32(0), 0);
  do_check_eq(row->AsInt64(0), 0);
  do_check_eq(row->AsDouble(0), 0.0);

  PRUint32 len = 100;
  do_check_eq(row->AsSharedUTF8String(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(row->AsSharedWString(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(row->AsSharedBlob(0, &len), NULL);
  do_check_eq(len, 0);

  do_check_eq(row->IsNull(0), true);
  return NS_OK;
}
Beispiel #8
0
void
test_Write()
{
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  char TEST_STRING[] = "this is a UTF-8 test!  This is pi: π";
  int TEST_LENGTH = strlen(TEST_STRING);
  Handle<String> str = String::New(TEST_STRING);
  char* buf = new char[TEST_LENGTH * 2];

  for (int start = 0; start < TEST_LENGTH; start++) {
    // Fill the buffer with 'a' to ensure there are no NULLs to start with.
    fill_string(buf, 'a', TEST_LENGTH * 2);
    int copied = str->WriteAscii(buf, start, TEST_LENGTH * 2);
    do_check_eq(copied, TEST_LENGTH - start);
    do_check_eq(strlen(buf), TEST_LENGTH - start);
  }

  delete[] buf;
  context.Dispose();
}
Beispiel #9
0
void
test_WriteUtf8()
{
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  char TEST_STRING[] = "this is a UTF-8 test!  This is pi: π";
  int TEST_LENGTH = strlen(TEST_STRING);
  Handle<String> str = String::New(TEST_STRING);
  char* buf = new char[TEST_LENGTH * 2];
  // Fill the buffer with 'a' to ensure there are no NULLs to start with.
  fill_string(buf, 'a', TEST_LENGTH * 2);
  int charsWritten;
  int copied = str->WriteUtf8(buf, TEST_LENGTH * 2, &charsWritten);
  do_check_eq(copied, TEST_LENGTH + 1);
  // π is 2 bytes, so strlen (TEST_LENGTH) returns 1 larger than charsWritten
  do_check_eq(charsWritten, TEST_LENGTH - 1);
  do_check_eq(strlen(buf), TEST_LENGTH);

  delete[] buf;
  context.Dispose();
}
Beispiel #10
0
  NS_IMETHOD Run()
  {
    mozilla::ReentrantMonitorAutoEnter lock(monitor);
    WaitFor(READ_LOCK);

    nsCString sql(mSQL);
    nsCOMPtr<mozIStorageStatement> stmt;
    do_check_success(mConnection->CreateStatement(sql, getter_AddRefs(stmt)));

    bool hasResult;
    nsresult rv = stmt->ExecuteStep(&hasResult);
    do_check_eq(rv, NS_ERROR_FILE_IS_LOCKED);

    // Finalize our statement and null out our connection before notifying to
    // ensure that we close on the proper thread.
    rv = stmt->Finalize();
    do_check_eq(rv, NS_ERROR_FILE_IS_LOCKED);
    mConnection = nullptr;

    Notify(TEST_DONE);

    return NS_OK;
  }
Beispiel #11
0
void
test_Length()
{
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  char TEST_STRING[] = "this is a test";
  int TEST_LENGTH = strlen(TEST_STRING);
  Handle<String> str = String::New(TEST_STRING);
  do_check_eq(str->Length(), TEST_LENGTH);

  // Now, make a string with an embedded NULL.
  TEST_STRING[8] = '\0';
  str = String::New(TEST_STRING, TEST_LENGTH);
  do_check_eq(str->Length(), TEST_LENGTH);

  // Finally, make sure that we end up calling strlen if no length is passed.
  str = String::New(TEST_STRING);

  int strlength = strlen(TEST_STRING);
  do_check_eq(str->Length(), strlength);
  context.Dispose();
}
Beispiel #12
0
void
test_BasicCall()
{
  HandleScope handle_scope;
  Handle<ObjectTemplate> templ = ObjectTemplate::New();
  Handle<FunctionTemplate> fnT = v8::FunctionTemplate::New(AddOne);
  templ->Set("AddOne", fnT);

  Persistent<Context> context = Context::New(NULL, templ);
  Local<Value> addone = context->Global()->Get(String::New("AddOne"));
  do_check_true(!addone.IsEmpty());
  do_check_true(!addone->IsUndefined());
  do_check_true(addone->IsObject());
  do_check_true(addone->IsFunction());
  Local<Function> fn = addone.As<Function>();
  do_check_eq(fn, fnT->GetFunction());
  Local<Number> n = Number::New(0.5);
  Handle<Value> args[] = { n };
  Local<Value> v = fn->Call(context->Global(), 1, args);
  do_check_true(!v.IsEmpty());
  do_check_true(v->IsNumber());
  Local<Number> n2 = v->ToNumber();
  do_check_eq(n2->Value(), 1.5);
}
Beispiel #13
0
void
test_AsciiValue_operators()
{
  HandleScope handle_scope;
  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  char TEST_STRING[] = "ascii string value";
  int TEST_LENGTH = strlen(TEST_STRING);
  Handle<String> str = String::New(TEST_STRING);
  do_check_eq(str->Length(), TEST_LENGTH);
  String::AsciiValue asciiString(str);
  const char* one = *asciiString;
  char* two = *asciiString;
  do_check_true(0 == strcmp(TEST_STRING, one));
  do_check_true(0 == strcmp(TEST_STRING, two));
  context.Dispose();
}
Beispiel #14
0
void
test_Name()
{
  HandleScope handle_scope;
  Handle<ObjectTemplate> templ = ObjectTemplate::New();
  Handle<FunctionTemplate> fnT = v8::FunctionTemplate::New(AddOne);
  templ->Set("AddOne", fnT);
  fnT->SetClassName(String::NewSymbol("AddOne"));

  Persistent<Context> context = Context::New(NULL, templ);
  Handle<Script> script = Script::New(String::New("AddOne.name;"));

  Context::Scope scope(context);
  Handle<Value> v = script->Run();
  do_check_true(!v.IsEmpty());
  Handle<String> s = v->ToString();
  do_check_eq(s, String::NewSymbol("AddOne"));
  context.Dispose();
}
Beispiel #15
0
void
test_obj_tmplexn() {
  HandleScope scope;

  Persistent<Context> context = Context::New();
  Context::Scope context_scope(context);

  Handle<ObjectTemplate> tmpl = ObjectTemplate::New();
  tmpl->SetNamedPropertyHandler(NamedGetterThrows, NamedSetterThrows, 0, NamedDeleterThrows);
  tmpl->SetIndexedPropertyHandler(IndexedGetterThrows, IndexedSetterThrows, 0, IndexedDeleterThrows);
  Handle<Object> obj = tmpl->NewInstance();
  Local<Object> global = context->Global();
  global->Set(String::New("testobj"), obj);

  Handle<String> source = String::New("var n = 0;"
                                      "try { testobj.myprop; } catch (e) { n += e; };"
                                      "try { testobj.myprop = (n+9); } catch (e) { n += e; }"
                                      "try { delete testobj.myprop; } catch (e) { n += e; };"
                                      "try { testobj[4]; } catch (e) { n += e; };"
                                      "try { testobj[5] = (n+9); } catch (e) { n += e; }"
                                      "try { delete testobj[6]; } catch (e) { n += e; }; n");

  // Compile the source code.
  Handle<Script> script = Script::Compile(source);

  TryCatch trycatch;
  // Run the script to get the result.
  Handle<Value> result = script->Run();

  do_check_false(result.IsEmpty());
  do_check_true(result->IsInt32());
  do_check_false(trycatch.HasCaught());
  JSInt32 i = result->Int32Value();
  do_check_eq(i, 21);
  context.Dispose();
}
void
test_NULLFallback()
{
  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());

  nsCOMPtr<mozIStorageStatement> stmt;
  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    "SELECT NULL"
  ), getter_AddRefs(stmt));

  nsCOMPtr<mozIStorageValueArray> valueArray = do_QueryInterface(stmt);
  do_check_true(valueArray);

  bool hasMore;
  do_check_true(NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)) && hasMore);

  do_check_eq(stmt->AsInt32(0), 0);
  do_check_eq(stmt->AsInt64(0), 0);
  do_check_eq(stmt->AsDouble(0), 0.0);
  PRUint32 len = 100;
  do_check_eq(stmt->AsSharedUTF8String(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(stmt->AsSharedWString(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(stmt->AsSharedBlob(0, &len), NULL);
  do_check_eq(len, 0);
  do_check_eq(stmt->IsNull(0), true);

  do_check_eq(valueArray->AsInt32(0), 0);
  do_check_eq(valueArray->AsInt64(0), 0);
  do_check_eq(valueArray->AsDouble(0), 0.0);
  len = 100;
  do_check_eq(valueArray->AsSharedUTF8String(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(valueArray->AsSharedWString(0, &len), NULL);
  do_check_eq(len, 0);
  len = 100;
  do_check_eq(valueArray->AsSharedBlob(0, &len), NULL);
  do_check_eq(len, 0);
  do_check_eq(valueArray->IsNull(0), true);
}