From ee41a3693ab8ae9547fbe1f11cc6fb31e7fb2c54 Mon Sep 17 00:00:00 2001
From: Tooru Fujisawa <arai_a@mac.com>
Date: Tue, 21 Sep 2021 18:45:17 +0000
Subject: [PATCH] Bug 1688791 - Part 4: Remove JS::DecodeScript. r=tcampbell

Differential Revision: https://phabricator.services.mozilla.com/D121270
---
 js/public/Transcoding.h        |  19 ----
 js/src/jsapi-tests/moz.build   |   1 -
 js/src/jsapi-tests/testXDR.cpp | 187 ---------------------------------
 js/src/jsapi.cpp               |  33 ------
 4 files changed, 240 deletions(-)
 delete mode 100644 js/src/jsapi-tests/testXDR.cpp

diff --git a/js/public/Transcoding.h b/js/public/Transcoding.h
index db725e7745feb..b1f06dbc5f857 100644
--- a/js/public/Transcoding.h
+++ b/js/public/Transcoding.h
@@ -98,25 +98,6 @@ extern JS_PUBLIC_API TranscodeResult EncodeScript(JSContext* cx,
                                                   TranscodeBuffer& buffer,
                                                   Handle<JSScript*> script);
 
-// Decode JSScript from the buffer.
-//
-// The start of `buffer` and `cursorIndex` should meet
-// IsTranscodingBytecodeAligned and IsTranscodingBytecodeOffsetAligned.
-// (This should be handled while encoding).
-extern JS_PUBLIC_API TranscodeResult
-DecodeScript(JSContext* cx, const ReadOnlyCompileOptions& options,
-             TranscodeBuffer& buffer, MutableHandle<JSScript*> scriptp,
-             size_t cursorIndex = 0);
-
-// Decode JSScript from the range.
-//
-// The start of `range` should meet IsTranscodingBytecodeAligned and
-// IsTranscodingBytecodeOffsetAligned.
-// (This should be handled while encoding).
-extern JS_PUBLIC_API TranscodeResult
-DecodeScript(JSContext* cx, const ReadOnlyCompileOptions& options,
-             const TranscodeRange& range, MutableHandle<JSScript*> scriptp);
-
 // Decode CompilationStencil from the buffer and instantiate JSScript from it.
 //
 // The start of `buffer` and `cursorIndex` should meet
diff --git a/js/src/jsapi-tests/moz.build b/js/src/jsapi-tests/moz.build
index ab85620504cac..c6d63d54d1dd2 100644
--- a/js/src/jsapi-tests/moz.build
+++ b/js/src/jsapi-tests/moz.build
@@ -127,7 +127,6 @@ UNIFIED_SOURCES += [
     "testWasmLEB128.cpp",
     "testWeakMap.cpp",
     "testWindowNonConfigurable.cpp",
-    "testXDR.cpp",
 ]
 
 SOURCES += [
diff --git a/js/src/jsapi-tests/testXDR.cpp b/js/src/jsapi-tests/testXDR.cpp
deleted file mode 100644
index d4c05c8d4a7f8..0000000000000
--- a/js/src/jsapi-tests/testXDR.cpp
+++ /dev/null
@@ -1,187 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
- * vim: set ts=8 sts=2 et sw=2 tw=80:
- * 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 "mozilla/Utf8.h"  // mozilla::Utf8Unit
-
-#include "js/BuildId.h"  // JS::BuildIdCharVector, JS::SetProcessBuildIdOp
-#include "js/CompilationAndEvaluation.h"  // JS::Compile
-#include "js/CompileOptions.h"            // JS::CompileOptions
-#include "js/SourceText.h"                // JS::Source{Ownership,Text}
-#include "js/Transcoding.h"
-#include "jsapi-tests/tests.h"
-#include "util/Text.h"
-#include "vm/JSScript.h"
-
-#include "vm/JSScript-inl.h"
-
-static bool GetBuildId(JS::BuildIdCharVector* buildId) {
-  const char buildid[] = "testXDR";
-  return buildId->append(buildid, sizeof(buildid));
-}
-
-static JSScript* FreezeThaw(JSContext* cx, JS::CompileOptions& options,
-                            JS::HandleScript script) {
-  JS::SetProcessBuildIdOp(::GetBuildId);
-
-  // freeze
-  JS::TranscodeBuffer buffer;
-  JS::TranscodeResult rs = JS::EncodeScript(cx, buffer, script);
-  if (rs != JS::TranscodeResult::Ok) {
-    return nullptr;
-  }
-
-  // thaw
-  JS::RootedScript script2(cx);
-  rs = JS::DecodeScript(cx, options, buffer, &script2);
-  if (rs != JS::TranscodeResult::Ok) {
-    return nullptr;
-  }
-  return script2;
-}
-
-enum TestCase {
-  TEST_FIRST,
-  TEST_SCRIPT = TEST_FIRST,
-  TEST_FUNCTION,
-  TEST_SERIALIZED_FUNCTION,
-  TEST_END
-};
-
-BEGIN_TEST(testXDR_bug506491) {
-  static const char s[] =
-      "function makeClosure(s, name, value) {\n"
-      "    eval(s);\n"
-      "    Math.sin(value);\n"
-      "    let n = name, v = value;\n"
-      "    return function () { return String(v); };\n"
-      "}\n"
-      "var f = makeClosure('0;', 'status', 'ok');\n";
-
-  // compile
-  JS::CompileOptions options(cx);
-  options.setFileAndLine(__FILE__, __LINE__);
-
-  JS::SourceText<mozilla::Utf8Unit> srcBuf;
-  CHECK(srcBuf.init(cx, s, js_strlen(s), JS::SourceOwnership::Borrowed));
-
-  JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf));
-  CHECK(script);
-
-  script = FreezeThaw(cx, options, script);
-  CHECK(script);
-
-  // execute
-  JS::RootedValue v2(cx);
-  CHECK(JS_ExecuteScript(cx, script, &v2));
-
-  // try to break the Block object that is the parent of f
-  JS_GC(cx);
-
-  // confirm
-  EVAL("f() === 'ok';\n", &v2);
-  JS::RootedValue trueval(cx, JS::TrueValue());
-  CHECK_SAME(v2, trueval);
-  return true;
-}
-END_TEST(testXDR_bug506491)
-
-BEGIN_TEST(testXDR_bug516827) {
-  // compile an empty script
-  JS::CompileOptions options(cx);
-  options.setFileAndLine(__FILE__, __LINE__);
-
-  JS::SourceText<mozilla::Utf8Unit> srcBuf;
-  CHECK(srcBuf.init(cx, "", 0, JS::SourceOwnership::Borrowed));
-
-  JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf));
-  CHECK(script);
-
-  script = FreezeThaw(cx, options, script);
-  CHECK(script);
-
-  // execute with null result meaning no result wanted
-  CHECK(JS_ExecuteScript(cx, script));
-  return true;
-}
-END_TEST(testXDR_bug516827)
-
-BEGIN_TEST(testXDR_source) {
-  const char* samples[] = {
-      // This can't possibly fail to compress well, can it?
-      "function f(x) { return x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + "
-      "x + x + x + x + x + x + x + x + x + x + x + x }",
-      "short", nullptr};
-  for (const char** s = samples; *s; s++) {
-    JS::CompileOptions options(cx);
-    options.setFileAndLine(__FILE__, __LINE__);
-
-    JS::SourceText<mozilla::Utf8Unit> srcBuf;
-    CHECK(srcBuf.init(cx, *s, strlen(*s), JS::SourceOwnership::Borrowed));
-
-    JS::RootedScript script(cx, JS::Compile(cx, options, srcBuf));
-    CHECK(script);
-
-    script = FreezeThaw(cx, options, script);
-    CHECK(script);
-
-    JSString* out = JS_DecompileScript(cx, script);
-    CHECK(out);
-
-    bool equal;
-    CHECK(JS_StringEqualsAscii(cx, out, *s, &equal));
-    CHECK(equal);
-  }
-  return true;
-}
-END_TEST(testXDR_source)
-
-BEGIN_TEST(testXDR_sourceMap) {
-  const char* sourceMaps[] = {"http://example.com/source-map.json",
-                              "file:///var/source-map.json", nullptr};
-  JS::RootedScript script(cx);
-  for (const char** sm = sourceMaps; *sm; sm++) {
-    JS::CompileOptions options(cx);
-    options.setFileAndLine(__FILE__, __LINE__);
-
-    JS::SourceText<mozilla::Utf8Unit> srcBuf;
-    CHECK(srcBuf.init(cx, "", 0, JS::SourceOwnership::Borrowed));
-
-    script = JS::Compile(cx, options, srcBuf);
-    CHECK(script);
-
-    size_t len = strlen(*sm);
-    JS::UniqueTwoByteChars expected_wrapper(js::InflateString(cx, *sm, len));
-    char16_t* expected = expected_wrapper.get();
-    CHECK(expected);
-
-    // The script source takes responsibility of free'ing |expected|.
-    CHECK(script->scriptSource()->setSourceMapURL(cx, expected));
-    script = FreezeThaw(cx, options, script);
-    CHECK(script);
-    CHECK(script->scriptSource());
-    CHECK(script->scriptSource()->hasSourceMapURL());
-
-    const char16_t* actual = script->scriptSource()->sourceMapURL();
-    CHECK(actual);
-
-    while (*expected) {
-      CHECK(*actual);
-      CHECK(*expected == *actual);
-      expected++;
-      actual++;
-    }
-    CHECK(!*actual);
-  }
-  return true;
-}
-END_TEST(testXDR_sourceMap)
diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp
index 7423c8b5ad9e4..465ba4aac9fbe 100644
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -4478,23 +4478,6 @@ JS_PUBLIC_API JS::TranscodeResult JS::EncodeScript(JSContext* cx,
   return JS::TranscodeResult::Ok;
 }
 
-JS_PUBLIC_API JS::TranscodeResult JS::DecodeScript(
-    JSContext* cx, const ReadOnlyCompileOptions& options,
-    TranscodeBuffer& buffer, JS::MutableHandleScript scriptp,
-    size_t cursorIndex) {
-  auto decoder = js::MakeUnique<XDRDecoder>(cx, &options, buffer, cursorIndex);
-  if (!decoder) {
-    ReportOutOfMemory(cx);
-    return JS::TranscodeResult::Throw;
-  }
-  XDRResult res = decoder->codeScript(scriptp);
-  MOZ_ASSERT(bool(scriptp) == res.isOk());
-  if (res.isErr()) {
-    return res.unwrapErr();
-  }
-  return JS::TranscodeResult::Ok;
-}
-
 static JS::TranscodeResult DecodeStencil(JSContext* cx,
                                          JS::TranscodeBuffer& buffer,
                                          frontend::CompilationInput& input,
@@ -4548,22 +4531,6 @@ JS_PUBLIC_API JS::TranscodeResult JS::DecodeScriptMaybeStencil(
   return JS::TranscodeResult::Ok;
 }
 
-JS_PUBLIC_API JS::TranscodeResult JS::DecodeScript(
-    JSContext* cx, const ReadOnlyCompileOptions& options,
-    const TranscodeRange& range, JS::MutableHandleScript scriptp) {
-  auto decoder = js::MakeUnique<XDRDecoder>(cx, &options, range);
-  if (!decoder) {
-    ReportOutOfMemory(cx);
-    return JS::TranscodeResult::Throw;
-  }
-  XDRResult res = decoder->codeScript(scriptp);
-  MOZ_ASSERT(bool(scriptp) == res.isOk());
-  if (res.isErr()) {
-    return res.unwrapErr();
-  }
-  return JS::TranscodeResult::Ok;
-}
-
 JS_PUBLIC_API JS::TranscodeResult JS::DecodeScriptAndStartIncrementalEncoding(
     JSContext* cx, const ReadOnlyCompileOptions& options,
     TranscodeBuffer& buffer, JS::MutableHandleScript scriptp,
-- 
GitLab