js/src/jsapi-tests/testCompileNonSyntactic.cpp
author Jeff Walden <jwalden@mit.edu>
Tue, 09 Apr 2019 03:50:03 +0000
changeset 468601 d470f958ea613268f839f8346e983dc9d8820c06
parent 451944 6686393af7c79f40b4fe8aae87c63fa196020a63
child 468603 3aa2e84843939738f07e6fa12983e1c52788d9a1
permissions -rw-r--r--
Bug 1542910 - Make all script compilation APIs directly return a JSScript*, rather than returning a success code and returning the JSScript* via outparam that exactly duplicates the success code value's meaning. r=arai Differential Revision: https://phabricator.services.mozilla.com/D26616

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gc/GCInternals.h"
#include "js/CompilationAndEvaluation.h"
#include "js/SourceText.h"
#include "jsapi-tests/tests.h"
#include "vm/Monitor.h"
#include "vm/MutexIDs.h"

using namespace JS;
using js::AutoLockMonitor;

struct OffThreadTask {
  OffThreadTask() : monitor(js::mutexid::ShellOffThreadState), token(nullptr) {}

  OffThreadToken* waitUntilDone(JSContext* cx) {
    if (OffThreadParsingMustWaitForGC(cx->runtime())) {
      js::gc::FinishGC(cx);
    }

    AutoLockMonitor alm(monitor);
    while (!token) {
      alm.wait();
    }
    OffThreadToken* result = token;
    token = nullptr;
    return result;
  }

  void markDone(JS::OffThreadToken* tokenArg) {
    AutoLockMonitor alm(monitor);
    token = tokenArg;
    alm.notify();
  }

  static void OffThreadCallback(OffThreadToken* token, void* context) {
    auto self = static_cast<OffThreadTask*>(context);
    self->markDone(token);
  }

  js::Monitor monitor;
  OffThreadToken* token;
};

BEGIN_TEST(testCompileScript) {
  CHECK(testCompile(true));

  CHECK(testCompile(false));
  return true;
}

bool testCompile(bool nonSyntactic) {
  static const char src[] = "42\n";
  static const char16_t src_16[] = u"42\n";

  constexpr size_t length = sizeof(src) - 1;
  static_assert(sizeof(src_16) / sizeof(*src_16) - 1 == length,
                "Source buffers must be same length");

  JS::CompileOptions options(cx);
  options.setNonSyntacticScope(nonSyntactic);

  JS::SourceText<char16_t> buf;
  CHECK(buf.init(cx, src_16, length, JS::SourceOwnership::Borrowed));

  JS::RootedScript script(cx);

  // Check explicit non-syntactic compilation first to make sure it doesn't
  // modify our options object.
  script = CompileForNonSyntacticScope(cx, options, buf);
  CHECK(script);
  CHECK_EQUAL(script->hasNonSyntacticScope(), true);

  script = CompileUtf8ForNonSyntacticScope(cx, options, src, length);
  CHECK(script);
  CHECK_EQUAL(script->hasNonSyntacticScope(), true);

  {
    JS::SourceText<char16_t> srcBuf;
    CHECK(srcBuf.init(cx, src_16, length, JS::SourceOwnership::Borrowed));

    script = CompileForNonSyntacticScope(cx, options, srcBuf);
    CHECK(script);
    CHECK_EQUAL(script->hasNonSyntacticScope(), true);
  }

  script = Compile(cx, options, buf);
  CHECK(script);
  CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);

  script = CompileUtf8(cx, options, src, length);
  CHECK(script);
  CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);

  {
    JS::SourceText<char16_t> srcBuf;
    CHECK(srcBuf.init(cx, src_16, length, JS::SourceOwnership::Borrowed));

    script = Compile(cx, options, srcBuf);
    CHECK(script);
    CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
  }

  options.forceAsync = true;
  OffThreadTask task;
  OffThreadToken* token;

  JS::SourceText<char16_t> srcBuf;
  CHECK(srcBuf.init(cx, src_16, length, JS::SourceOwnership::Borrowed));

  CHECK(CompileOffThread(cx, options, srcBuf, task.OffThreadCallback, &task));
  CHECK(token = task.waitUntilDone(cx));
  CHECK(script = FinishOffThreadScript(cx, token));
  CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);

  return true;
}
END_TEST(testCompileScript);