js/src/jsapi-tests/testFunctionBinding.cpp
author Jon Coppeard <jcoppeard@mozilla.com>
Mon, 14 Jan 2019 11:02:35 +0000
changeset 453704 1cebf4f5c850ad22500a360c2742de3c9dd78c81
parent 448963 66eb1f485c1a3ea81372758bc92292c9428b17cd
child 466295 66414629b2e353ef5740bd57da3a8ffdf39e5ce0
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:
 *
 * Test function name binding.
 */
/* 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 "jsfriendapi.h"

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

using namespace js;

BEGIN_TEST(test_functionBinding) {
  RootedFunction fun(cx);

  JS::CompileOptions options(cx);
  options.setFileAndLine(__FILE__, __LINE__);

  // Named function shouldn't have it's binding.
  const char s1chars[] = "return (typeof s1) == 'undefined';";
  JS::AutoObjectVector emptyScopeChain(cx);
  CHECK(JS::CompileFunctionUtf8(cx, emptyScopeChain, options, "s1", 0, nullptr,
                                s1chars, strlen(s1chars), &fun));
  CHECK(fun);

  JS::AutoValueVector args(cx);
  RootedValue rval(cx);
  CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval));
  CHECK(rval.isBoolean());
  CHECK(rval.toBoolean());

  // Named function shouldn't have `anonymous` binding.
  const char s2chars[] = "return (typeof anonymous) == 'undefined';";
  CHECK(JS::CompileFunctionUtf8(cx, emptyScopeChain, options, "s2", 0, nullptr,
                                s2chars, strlen(s2chars), &fun));
  CHECK(fun);

  CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval));
  CHECK(rval.isBoolean());
  CHECK(rval.toBoolean());

  // Anonymous function shouldn't have `anonymous` binding.
  const char s3chars[] = "return (typeof anonymous) == 'undefined';";
  CHECK(JS::CompileFunctionUtf8(cx, emptyScopeChain, options, nullptr, 0,
                                nullptr, s3chars, strlen(s3chars), &fun));
  CHECK(fun);

  CHECK(JS::Call(cx, UndefinedHandleValue, fun, args, &rval));
  CHECK(rval.isBoolean());
  CHECK(rval.toBoolean());

  return true;
}
END_TEST(test_functionBinding)