[gjs: 9/25] js: Remove handling of shebang line



commit 1884347ea8812645b0d79b2ca3482e96579f5634
Author: Philip Chimento <philip chimento gmail com>
Date:   Wed Oct 23 15:04:56 2019 -0700

    js: Remove handling of shebang line
    
    SpiderMonkey now removes shebang lines itself in all scripts that it
    compiles, so there's no need for us to have code that does the same
    thing.

 gjs/context.cpp    |  8 ++------
 gjs/jsapi-util.cpp | 32 --------------------------------
 gjs/jsapi-util.h   |  3 ---
 gjs/module.cpp     |  8 ++------
 test/gjs-tests.cpp | 42 ------------------------------------------
 5 files changed, 4 insertions(+), 89 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 52e39b97..b2b6b099 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -975,11 +975,7 @@ bool GjsContextPrivate::eval_with_scope(JS::HandleObject scope_object,
 
     std::u16string utf16_string = gjs_utf8_script_to_utf16(script, script_len);
 
-    unsigned start_line_number = 1;
-    size_t offset = gjs_unix_shebang_len(utf16_string, &start_line_number);
-
-    JS::SourceBufferHolder buf(utf16_string.c_str() + offset,
-                               utf16_string.size() - offset,
+    JS::SourceBufferHolder buf(utf16_string.c_str(), utf16_string.size(),
                                JS::SourceBufferHolder::NoOwnership);
 
     JS::RootedObjectVector scope_chain(m_cx);
@@ -989,7 +985,7 @@ bool GjsContextPrivate::eval_with_scope(JS::HandleObject scope_object,
     }
 
     JS::CompileOptions options(m_cx);
-    options.setFileAndLine(filename, start_line_number);
+    options.setFileAndLine(filename, 1);
 
     if (!JS::Evaluate(m_cx, scope_chain, options, buf, retval))
         return false;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 4a0648ae..1993307e 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -577,38 +577,6 @@ gjs_maybe_gc (JSContext *context)
     gjs_gc_if_needed(context);
 }
 
-/**
- * gjs_unix_shebang_len:
- *
- * @script: A JS script
- * @start_line_number: (out): the new start-line number to account for the
- * offset as a result of stripping the shebang; can be either 1 or 2.
- *
- * Returns the offset in @script where the actual script begins with Unix
- * shebangs removed. The outparam is useful to know what line of the
- * original script we're executing from, so that any relevant
- * offsets can be applied to the results of an execution pass.
- */
-size_t gjs_unix_shebang_len(const std::u16string& script,
-                            unsigned* start_line_number) {
-    g_assert(start_line_number);
-
-    if (script.compare(0, 2, u"#!") != 0) {
-        // No shebang, leave the script unchanged
-        *start_line_number = 1;
-        return 0;
-    }
-
-    *start_line_number = 2;
-
-    size_t newline_pos = script.find('\n', 2);
-    if (newline_pos == std::u16string::npos)
-        return script.size();  // Script consists only of a shebang line
-
-    // Point the offset after the newline
-    return newline_pos + 1;
-}
-
 /**
  * gjs_get_import_global:
  * @context: a #JSContext
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 0a6be3b5..f011a395 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -296,9 +296,6 @@ bool        gjs_unichar_from_string          (JSContext       *context,
 void gjs_maybe_gc (JSContext *context);
 void gjs_gc_if_needed(JSContext *cx);
 
-GJS_USE
-size_t gjs_unix_shebang_len(const std::u16string& script,
-                            unsigned* start_line_number);
 GJS_USE
 std::u16string gjs_utf8_script_to_utf16(const char* script, ssize_t len);
 
diff --git a/gjs/module.cpp b/gjs/module.cpp
index f66e8540..f08cb644 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -98,11 +98,7 @@ class GjsModule {
         std::u16string utf16_string =
             gjs_utf8_script_to_utf16(script, script_len);
 
-        unsigned start_line_number = 1;
-        size_t offset = gjs_unix_shebang_len(utf16_string, &start_line_number);
-
-        JS::SourceBufferHolder buf(utf16_string.c_str() + offset,
-                                   utf16_string.size() - offset,
+        JS::SourceBufferHolder buf(utf16_string.c_str(), utf16_string.size(),
                                    JS::SourceBufferHolder::NoOwnership);
 
         JS::RootedObjectVector scope_chain(cx);
@@ -112,7 +108,7 @@ class GjsModule {
         }
 
         JS::CompileOptions options(cx);
-        options.setFileAndLine(filename, start_line_number);
+        options.setFileAndLine(filename, 1);
 
         JS::RootedValue ignored_retval(cx);
         if (!JS::Evaluate(cx, scope_chain, options, buf, &ignored_retval))
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 45ebe243..0fa8d75d 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -325,42 +325,6 @@ gjstest_test_func_util_misc_strv_concat_pointers(void)
     g_strfreev(ret);
 }
 
-static void
-gjstest_test_strip_shebang_no_advance_for_no_shebang(void)
-{
-    unsigned line_number = 1;
-    size_t offset = gjs_unix_shebang_len(u"foo\nbar", &line_number);
-
-    g_assert_cmpuint(offset, ==, 0);
-    g_assert_cmpuint(line_number, ==, 1);
-}
-
-static void gjstest_test_strip_shebang_no_advance_for_too_short_string(void) {
-    unsigned line_number = 1;
-    size_t offset = gjs_unix_shebang_len(u"Z", &line_number);
-
-    g_assert_cmpuint(offset, ==, 0);
-    g_assert_cmpuint(line_number, ==, 1);
-}
-
-static void
-gjstest_test_strip_shebang_advance_for_shebang(void)
-{
-    unsigned line_number = 1;
-    size_t offset = gjs_unix_shebang_len(u"#!foo\nbar", &line_number);
-
-    g_assert_cmpuint(offset, ==, 6);
-    g_assert_cmpuint(line_number, ==, 2);
-}
-
-static void gjstest_test_strip_shebang_advance_to_end_for_just_shebang(void) {
-    unsigned line_number = 1;
-    size_t offset = gjs_unix_shebang_len(u"#!foo", &line_number);
-
-    g_assert_cmpuint(offset, ==, 5);
-    g_assert_cmpuint(line_number, ==, 2);
-}
-
 static void
 gjstest_test_profiler_start_stop(void)
 {
@@ -403,12 +367,6 @@ main(int    argc,
                     gjstest_test_func_gjs_context_eval_non_zero_terminated);
     g_test_add_func("/gjs/context/exit", gjstest_test_func_gjs_context_exit);
     g_test_add_func("/gjs/gobject/js_defined_type", gjstest_test_func_gjs_gobject_js_defined_type);
-    g_test_add_func("/gjs/jsutil/strip_shebang/no_shebang", 
gjstest_test_strip_shebang_no_advance_for_no_shebang);
-    g_test_add_func("/gjs/jsutil/strip_shebang/short_string",
-                    gjstest_test_strip_shebang_no_advance_for_too_short_string);
-    g_test_add_func("/gjs/jsutil/strip_shebang/have_shebang", 
gjstest_test_strip_shebang_advance_for_shebang);
-    g_test_add_func("/gjs/jsutil/strip_shebang/only_shebang",
-                    gjstest_test_strip_shebang_advance_to_end_for_just_shebang);
     g_test_add_func("/gjs/profiler/start_stop", gjstest_test_profiler_start_stop);
     g_test_add_func("/util/misc/strv/concat/null",
                     gjstest_test_func_util_misc_strv_concat_null);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]