js/src/jsapi-tests/testJSEvaluateScript.cpp
author Jeff Walden <jwalden@mit.edu>
Thu, 08 Nov 2018 18:42:48 -0800
changeset 502944 56eaf6c976d377cba4e44a302bdeda9e7420bd94
parent 502936 c489ba287b497b77e6bb7d4b7550e0792670726c
child 505383 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1485800 - Rename SourceBufferHolder to SourceText, and add a <typename Unit> template parameter to it so it can hold putative UTF-8 or UTF-16 source text. r=tcampbell, r=fitzgen

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

#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)