Skip to content

Commit

Permalink
Merge pull request #3062 from ruby/reverse-sync
Browse files Browse the repository at this point in the history
Reverse sync
  • Loading branch information
kddnewton authored Sep 13, 2024
2 parents 0684241 + 0b527ca commit 7d14ed3
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 92 deletions.
9 changes: 9 additions & 0 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ID rb_id_option_encoding;
ID rb_id_option_filepath;
ID rb_id_option_frozen_string_literal;
ID rb_id_option_line;
ID rb_id_option_main_script;
ID rb_id_option_scopes;
ID rb_id_option_version;
ID rb_id_source_for;
Expand Down Expand Up @@ -179,6 +180,8 @@ build_options_i(VALUE key, VALUE value, VALUE argument) {

pm_options_command_line_set(options, command_line);
}
} else if (key_id == rb_id_option_main_script) {
if (!NIL_P(value)) pm_options_main_script_set(options, RTEST(value));
} else {
rb_raise(rb_eArgError, "unknown keyword: %" PRIsVALUE, key);
}
Expand Down Expand Up @@ -753,6 +756,11 @@ parse_input(pm_string_t *input, const pm_options_t *options) {
* has been set. This should be a boolean or nil.
* * `line` - the line number that the parse starts on. This should be an
* integer or nil. Note that this is 1-indexed.
* * `main_script` - a boolean indicating whether or not the source being parsed
* is the main script being run by the interpreter. This controls whether
* or not shebangs are parsed for additional flags and whether or not the
* parser will attempt to find a matching shebang if the first one does
* not contain the word "ruby".
* * `scopes` - the locals that are in scope surrounding the code that is being
* parsed. This should be an array of arrays of symbols or nil. Scopes are
* ordered from the outermost scope to the innermost one.
Expand Down Expand Up @@ -1165,6 +1173,7 @@ Init_prism(void) {
rb_id_option_filepath = rb_intern_const("filepath");
rb_id_option_frozen_string_literal = rb_intern_const("frozen_string_literal");
rb_id_option_line = rb_intern_const("line");
rb_id_option_main_script = rb_intern_const("main_script");
rb_id_option_scopes = rb_intern_const("scopes");
rb_id_option_version = rb_intern_const("version");
rb_id_source_for = rb_intern("for");
Expand Down
15 changes: 15 additions & 0 deletions include/prism/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ typedef struct pm_options {
* but ignore any encoding magic comments at the top of the file.
*/
bool encoding_locked;

/**
* When the file being parsed is the main script, the shebang will be
* considered for command-line flags (or for implicit -x). The caller needs
* to pass this information to the parser so that it can behave correctly.
*/
bool main_script;
} pm_options_t;

/**
Expand Down Expand Up @@ -248,6 +255,14 @@ PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options,
*/
PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const char *version, size_t length);

/**
* Set the main script option on the given options struct.
*
* @param options The options struct to set the main script value on.
* @param main_script The main script value to set.
*/
PRISM_EXPORTED_FUNCTION void pm_options_main_script_set(pm_options_t *options, bool main_script);

/**
* Allocate and zero out the scopes array on the given options struct.
*
Expand Down
2 changes: 2 additions & 0 deletions java-wasm/src/test/java/org/prism/DummyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void test1() {
EnumSet.noneOf(ParsingOptions.CommandLine.class),
ParsingOptions.SyntaxVersion.LATEST,
false,
false,
new byte[][][] {}
);

Expand Down Expand Up @@ -93,6 +94,7 @@ public void test2() {
EnumSet.noneOf(ParsingOptions.CommandLine.class),
ParsingOptions.SyntaxVersion.LATEST,
false,
false,
new byte[][][] {}
);

Expand Down
6 changes: 5 additions & 1 deletion java/org/prism/ParsingOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ public enum CommandLine { A, E, L, N, P, X };
* @param commandLine the set of flags that were set on the command line
* @param version code of Ruby version which syntax will be used to parse
* @param encodingLocked whether the encoding is locked (should almost always be false)
* @param mainScript whether the file is the main script
* @param scopes scopes surrounding the code that is being parsed with local variable names defined in every scope
* ordered from the outermost scope to the innermost one
*/
public static byte[] serialize(byte[] filepath, int line, byte[] encoding, boolean frozenStringLiteral, EnumSet<CommandLine> commandLine, SyntaxVersion version, boolean encodingLocked, byte[][][] scopes) {
public static byte[] serialize(byte[] filepath, int line, byte[] encoding, boolean frozenStringLiteral, EnumSet<CommandLine> commandLine, SyntaxVersion version, boolean encodingLocked, boolean mainScript, byte[][][] scopes) {
final ByteArrayOutputStream output = new ByteArrayOutputStream();

// filepath
Expand All @@ -73,6 +74,9 @@ public static byte[] serialize(byte[] filepath, int line, byte[] encoding, boole
// encodingLocked
output.write(encodingLocked ? 1 : 0);

// mainScript
output.write(mainScript ? 1 : 0);

// scopes

// number of scopes
Expand Down
5 changes: 4 additions & 1 deletion javascript/src/parsePrism.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function dumpOptions(options) {
}

template.push("C");
values.push(options.frozen_string_literal === undefined ? 0 : 1);
values.push((options.frozen_string_literal === undefined || options.frozen_string_literal === false || options.frozen_string_literal === null) ? 0 : 1);

template.push("C");
values.push(dumpCommandLineOptions(options));
Expand All @@ -109,6 +109,9 @@ function dumpOptions(options) {
template.push("C");
values.push(options.encoding === false ? 1 : 0);

template.push("C");
values.push((options.main_script === undefined || options.main_script === false || options.main_script === null) ? 0 : 1);

template.push("L");
if (options.scopes) {
const scopes = options.scopes;
Expand Down
3 changes: 3 additions & 0 deletions lib/prism/ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ def dump_options(options)
template << "C"
values << (options[:encoding] == false ? 1 : 0)

template << "C"
values << (options.fetch(:main_script, false) ? 1 : 0)

template << "L"
if (scopes = options[:scopes])
values << scopes.length
Expand Down
Loading

0 comments on commit 7d14ed3

Please sign in to comment.