tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testFreshGlobalEvalRedefinition.cpp (2267B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 */
      4 /* This Source Code Form is subject to the terms of the Mozilla Public
      5 * License, v. 2.0. If a copy of the MPL was not distributed with this
      6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      7 
      8 #include "mozilla/Utf8.h"  // mozilla::Utf8Unit
      9 
     10 #include "js/CompilationAndEvaluation.h"  // JS::Evaluate
     11 #include "js/GlobalObject.h"              // JS_NewGlobalObject
     12 #include "js/PropertyAndElement.h"        // JS_GetProperty
     13 #include "js/SourceText.h"                // JS::Source{Ownership,Text}
     14 #include "jsapi-tests/tests.h"
     15 #include "util/Text.h"
     16 
     17 static bool GlobalResolve(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
     18                          bool* resolvedp) {
     19  return JS_ResolveStandardClass(cx, obj, id, resolvedp);
     20 }
     21 
     22 BEGIN_TEST(testRedefineGlobalEval) {
     23  static const JSClassOps clsOps = {
     24      nullptr,                         // addProperty
     25      nullptr,                         // delProperty
     26      nullptr,                         // enumerate
     27      JS_NewEnumerateStandardClasses,  // newEnumerate
     28      GlobalResolve,                   // resolve
     29      nullptr,                         // mayResolve
     30      nullptr,                         // finalize
     31      nullptr,                         // call
     32      nullptr,                         // construct
     33      JS_GlobalObjectTraceHook,        // trace
     34  };
     35 
     36  static const JSClass cls = {
     37      "global",
     38      JSCLASS_GLOBAL_FLAGS,
     39      &clsOps,
     40  };
     41 
     42  /* Create the global object. */
     43  JS::RealmOptions options;
     44  JS::Rooted<JSObject*> g(
     45      cx,
     46      JS_NewGlobalObject(cx, &cls, nullptr, JS::FireOnNewGlobalHook, options));
     47  if (!g) {
     48    return false;
     49  }
     50 
     51  JSAutoRealm ar(cx, g);
     52  JS::Rooted<JS::Value> v(cx);
     53  CHECK(JS_GetProperty(cx, g, "Object", &v));
     54 
     55  static const char data[] =
     56      "Object.defineProperty(this, 'eval', { configurable: false });";
     57 
     58  JS::CompileOptions opts(cx);
     59 
     60  JS::SourceText<mozilla::Utf8Unit> srcBuf;
     61  CHECK(srcBuf.init(cx, data, js_strlen(data), JS::SourceOwnership::Borrowed));
     62 
     63  CHECK(JS::Evaluate(cx, opts.setFileAndLine(__FILE__, __LINE__), srcBuf, &v));
     64 
     65  return true;
     66 }
     67 END_TEST(testRedefineGlobalEval)