Class: Ferrum::Frame::Script Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/frame/script.rb,
sig/ferrum/frame/script.rbs

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A script passed to Runtime#evaluate and friends. Reads just enough JavaScript to tell a function declaration from an expression and to find where the code and its statements end, skipping strings, template literals, regular expressions and comments so that a // inside "http://…" isn't mistaken for one.

The source is scanned once, in #initialize, keeping a stack of open brackets and template literals. On the way it records where the code starts and ends, where the first top-level (…) opens and closes, where the first top-level {…} closes, where the last statement starts, and whether await appears. Every question below is answered from those positions.

Examples:

script = Script.new("// sum\nasync function(a, b = 1) { return a + b };")
script.code          # => "async function(a, b = 1) { return a + b }"
script.declaration?  # => true
script.awaits?       # => false

Constant Summary collapse

IDENTIFIER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

A JavaScript identifier, Unicode letters included.

Returns:

  • (::Regexp)
/[\p{L}\p{Nl}_$][\p{L}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}$]*/
FUNCTION_DECLARATION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/\A(?:async\s+)?function[\s*(]/
SINGLE_PARAMETER_ARROW =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/\A(?:async\s+)?#{IDENTIFIER}\s*=>/
ASYNC_PREFIX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/\A(?:async\s*)?\z/
ARROW =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/\A\s*=>/
COMMENT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
%r{//[^\n]*|/\*.*?(?:\*/|\z)}m
NOISE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/\s+|#{COMMENT}/
STRING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/"(?:\\.|[^"\\\n])*"?|'(?:\\.|[^'\\\n])*'?/m
TEMPLATE_CHUNK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/(?:\\.|[^`\\$]|\$(?!\{))*/m
REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
%r{/(?:\\.|\[(?:\\.|[^\]\\\n])*\]|[^/\\\n\[])+/[A-Za-z]*}
WORD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Regexp)
/[\p{Alnum}_$]+/
VALUE_END =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Characters ending a value, after which a / is a division.

Returns:

  • (::Regexp)
%r{[\p{Alnum}_$)\]\}"'`/]}
REGEXP_KEYWORDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Words after which a / starts a regular expression, not a division.

Returns:

  • (::Array[::String])
%w[return typeof instanceof in of new delete void throw case do else yield await].freeze
CONTINUING_WORDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Words that continue the statement on the line before them.

Returns:

  • (::Array[::String])
%w[in instanceof of else catch finally].freeze
INCREMENTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

x++ ends a value, and ++x starts one.

%w[++ --].freeze
CONTROL_WORDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Words whose condition in parentheses is followed by the statement they control, which may start on the next line.

%w[if for while with].freeze
OBJECT_KEYWORDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Words after which a { opens an object, not a block.

(REGEXP_KEYWORDS - %w[do else]).freeze
STATEMENT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

A statement that isn't an expression, so it has no value to return.

Returns:

  • (::Regexp)
/\A(?:const|let|var|if|for|while|do|switch|try|return|throw|break|continue)\b/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Script

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Script.

Parameters:

  • source (String)


77
78
79
80
81
82
83
84
85
# File 'lib/ferrum/frame/script.rb', line 77

def initialize(source)
  @source = source.to_s
  @code_start = @code_end = 0
  @awaits = false
  @scanned = @source.valid_encoding? && (@source.encoding == Encoding::UTF_8 || @source.ascii_only?)
  @scanned ? scan : @code_end = @source.bytesize
  @code = @source.byteslice(@code_start...@code_end)
  @wrapped = Script.new(@code[1...-1]) if parenthesized?
end

Instance Attribute Details

#code ⇒ String (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the script without surrounding whitespace, comments and trailing semicolons.

Returns:

  • (String) —

    the script without surrounding whitespace, comments and trailing semicolons.



72
73
74
# File 'lib/ferrum/frame/script.rb', line 72

def code
  @code
end

#source ⇒ String (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the script as given.

Returns:

  • (String) —

    the script as given.



68
69
70
# File 'lib/ferrum/frame/script.rb', line 68

def source
  @source
end

Instance Method Details

#awaits? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the script uses await, and so has to run in an async function.

Returns:

  • (Boolean)


107
108
109
# File 'lib/ferrum/frame/script.rb', line 107

def awaits?
  @awaits
end

#declaration? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the script is a complete function or arrow declaration, rather than an expression that happens to begin with one, such as the immediately invoked function() { … }().

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/ferrum/frame/script.rb', line 94

def declaration?
  return false unless @scanned
  return @wrapped.declaration? if @wrapped

  arrow? || (@code.match?(FUNCTION_DECLARATION) && @body_end == @code_end)
end

#split_last_statement ⇒ Array(String, String)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The code split before its last statement, so that the value of that one can be returned. Statements are separated by a top-level semicolon, as in "a(); b", or by a line break that can only end a statement, as in "a = 1\nb", which gives ["a = 1\n", "b"].

Returns:

  • (Array(String, String))


119
120
121
122
123
# File 'lib/ferrum/frame/script.rb', line 119

def split_last_statement
  return ["", @code] unless @last_statement_start

  [@source.byteslice(@code_start...@last_statement_start), @source.byteslice(@last_statement_start...@code_end)]
end