Class: Code
- Inherits:
-
Object
show all
- Defined in:
- lib/code.rb,
lib/code/node.rb,
lib/code/type.rb,
lib/code/error.rb,
lib/code/format.rb,
lib/code/object.rb,
lib/code/parser.rb,
lib/code/network.rb,
lib/code/node/if.rb,
lib/code/type/or.rb,
lib/code/concerns.rb,
lib/code/node/not.rb,
lib/code/type/sig.rb,
lib/code/node/call.rb,
lib/code/node/code.rb,
lib/code/node/list.rb,
lib/code/type/hash.rb,
lib/code/node/splat.rb,
lib/code/node/while.rb,
lib/code/object/ics.rb,
lib/code/object/url.rb,
lib/code/type/maybe.rb,
lib/code/node/base_2.rb,
lib/code/node/base_8.rb,
lib/code/node/number.rb,
lib/code/node/string.rb,
lib/code/object/code.rb,
lib/code/object/date.rb,
lib/code/object/html.rb,
lib/code/object/http.rb,
lib/code/object/json.rb,
lib/code/object/list.rb,
lib/code/object/smtp.rb,
lib/code/object/time.rb,
lib/code/type/repeat.rb,
lib/code/node/base_10.rb,
lib/code/node/base_16.rb,
lib/code/node/boolean.rb,
lib/code/node/decimal.rb,
lib/code/node/nothing.rb,
lib/code/node/ternary.rb,
lib/code/object/class.rb,
lib/code/object/range.rb,
lib/code/object/super.rb,
lib/code/node/function.rb,
lib/code/node/negation.rb,
lib/code/object/global.rb,
lib/code/object/number.rb,
lib/code/object/string.rb,
lib/code/node/statement.rb,
lib/code/object/base_64.rb,
lib/code/object/boolean.rb,
lib/code/object/context.rb,
lib/code/object/decimal.rb,
lib/code/object/integer.rb,
lib/code/object/nothing.rb,
lib/code/concerns/shared.rb,
lib/code/node/dictionary.rb,
lib/code/object/duration.rb,
lib/code/object/function.rb,
lib/code/node/unary_minus.rb,
lib/code/object/parameter.rb,
lib/code/object/dictionary.rb,
lib/code/node/call_argument.rb,
lib/code/node/left_operation.rb,
lib/code/node/square_bracket.rb,
lib/code/object/cryptography.rb,
lib/code/node/right_operation.rb,
lib/code/object/identifier_list.rb,
lib/code/node/function_parameter.rb
Defined Under Namespace
Modules: Concerns, Network
Classes: Error, Format, Node, Object, Parser, Type
Constant Summary
collapse
- GLOBALS =
%i[context error input object output root_object source].freeze
- DEFAULT_TIMEOUT =
1.hour.to_f
- MAX_INPUT_BYTES =
10.megabytes
- LOCALES =
%w[en fr].freeze
- Version =
Gem::Version.new(File.read(File.expand_path("../../VERSION", __dir__)))
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(source, context: Object::Context.new, error: StringIO.new, input: StringIO.new, object: Object::Global.new, output: StringIO.new, timeout: DEFAULT_TIMEOUT) ⇒ Code
Returns a new instance of Code.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/code.rb', line 9
def initialize(
source,
context: Object::Context.new,
error: StringIO.new,
input: StringIO.new,
object: Object::Global.new,
output: StringIO.new,
timeout: DEFAULT_TIMEOUT
)
@context = context
@error = error
@input = input
@object = object
@output = output
@source = source
@timeout = self.class.normalize_timeout!(timeout)
end
|
Class Method Details
53
54
55
56
57
|
# File 'lib/code.rb', line 53
def self.ensure_input_size!(source, limit: MAX_INPUT_BYTES, label: "input")
return if source.to_s.bytesize <= limit
raise Error, "#{label} is too large"
end
|
38
39
40
|
# File 'lib/code.rb', line 38
def self.evaluate(...)
new(...).evaluate
end
|
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/code.rb', line 42
def self.format(source_or_tree, timeout: DEFAULT_TIMEOUT)
parse_tree =
if source_or_tree.is_a?(::String)
parse(source_or_tree, timeout: timeout)
else
source_or_tree
end
Format.format(parse_tree)
end
|
.normalize_timeout!(timeout) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/code.rb', line 59
def self.normalize_timeout!(timeout)
timeout = DEFAULT_TIMEOUT if timeout.nil?
timeout = timeout.to_f
raise Error, "timeout must be positive" unless timeout.positive?
timeout
end
|
.parse(source, timeout: DEFAULT_TIMEOUT) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/code.rb', line 27
def self.parse(source, timeout: DEFAULT_TIMEOUT)
timeout = normalize_timeout!(timeout)
ensure_input_size!(source, label: "source")
Timeout.timeout(timeout) { Parser.parse(source).to_raw }
rescue Timeout::Error
raise Error, "timeout"
rescue SystemStackError
raise Error, "source is too deeply nested"
end
|
Instance Method Details
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/code.rb', line 67
def evaluate
time_zone = ::Time.zone
Timeout.timeout(timeout) do
Node::Code.new(Code.parse(source)).evaluate(
context: context,
error: error,
global_control_flow_root: true,
input: input,
object: object,
output: output,
root_object: object,
source: source,
timeout: timeout
)
end
rescue Timeout::Error
raise Error, "timeout"
rescue Interrupt
raise Error, "interrupt"
rescue SystemStackError
raise Error, "source is too deeply nested"
ensure
::Time.zone = time_zone
end
|