js/src/jsapi-tests/testJSEvaluateScript.cpp
author Jon Coppeard <jcoppeard@mozilla.com>
Mon, 14 Jan 2019 11:02:35 +0000
changeset 453704 1cebf4f5c850ad22500a360c2742de3c9dd78c81
parent 448963 66eb1f485c1a3ea81372758bc92292c9428b17cd
child 466300 9e33009abc2a84be589ebc0d301fabc865cb92cb
permissions -rw-r--r--
Bug 1519397 - Factor GC locking RAII classes out of vm/Runtime.h r=pbone

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */

#include "js/CompilationAndEvaluation.h"
#include "js/SourceText.h"
#include "jsapi-tests/tests.h"

using mozilla::ArrayLength;

BEGIN_TEST(testJSEvaluateScript) {
  JS::RootedObject obj(cx, JS_NewPlainObject(cx));
  CHECK(obj);

  static const char16_t src[] = u"var x = 5;";

  JS::RootedValue retval(cx);
  JS::CompileOptions opts(cx);
  JS::AutoObjectVector scopeChain(cx);
  CHECK(scopeChain.append(obj));

  JS::SourceText<char16_t> srcBuf;
  CHECK(srcBuf.init(cx, src, ArrayLength(src) - 1,
                    JS::SourceOwnership::Borrowed));

  CHECK(JS::Evaluate(cx, scopeChain, opts.setFileAndLine(__FILE__, __LINE__),
                     srcBuf, &retval));

  bool hasProp = true;
  CHECK(JS_AlreadyHasOwnProperty(cx, obj, "x", &hasProp));
  CHECK(hasProp);

  hasProp = false;
  CHECK(JS_HasProperty(cx, global, "x", &hasProp));
  CHECK(!hasProp);

  return true;
}
END_TEST(testJSEvaluateScript)