instant_record 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
require "prism"
|
|
2
|
+
require_relative "../native/contract"
|
|
3
|
+
require_relative "runner"
|
|
4
|
+
|
|
5
|
+
module InstantRecord
|
|
6
|
+
class Compiler
|
|
7
|
+
# Statically extracts `InstantRecord::Native::Contract` definitions from
|
|
8
|
+
# `app/screens/**/*.rb` using Ruby's Prism parser without booting Rails or
|
|
9
|
+
# evaluating application code.
|
|
10
|
+
module NativeContracts
|
|
11
|
+
SCALAR_METHODS = %i[string int float bool date].freeze
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def extract_dir(screens_dir)
|
|
16
|
+
return [] unless screens_dir && File.directory?(screens_dir)
|
|
17
|
+
|
|
18
|
+
contracts = []
|
|
19
|
+
seen = {}
|
|
20
|
+
|
|
21
|
+
Dir.glob("**/*.rb", base: screens_dir).sort.each do |rel|
|
|
22
|
+
path = File.join(screens_dir, rel)
|
|
23
|
+
extract_file(path).each do |contract|
|
|
24
|
+
if (previous = seen[contract.component])
|
|
25
|
+
raise BuildError,
|
|
26
|
+
"Duplicate native contract #{contract.component.inspect} in #{path} (already declared in #{previous})"
|
|
27
|
+
end
|
|
28
|
+
seen[contract.component] = path
|
|
29
|
+
contracts << contract
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
contracts.sort_by(&:component)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def extract_file(path)
|
|
37
|
+
extract_source(File.read(path), path: path)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def extract_source(source, path: "<screen>")
|
|
41
|
+
parsed = Prism.parse(source)
|
|
42
|
+
unless parsed.success?
|
|
43
|
+
details = parsed.errors.map { |err| "#{path}:#{err.location.start_line}: #{err.message}" }.join("\n ")
|
|
44
|
+
raise BuildError, "Could not parse native screen for contract codegen:\n #{details}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
Extractor.new(path).extract(parsed.value)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Walks a Prism AST to extract class-level `contract` blocks.
|
|
51
|
+
class Extractor
|
|
52
|
+
def initialize(path)
|
|
53
|
+
@path = path
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def extract(root)
|
|
57
|
+
contracts = []
|
|
58
|
+
walk(root, [], contracts)
|
|
59
|
+
contracts
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def walk(node, namespace, contracts)
|
|
65
|
+
return unless node
|
|
66
|
+
|
|
67
|
+
case node
|
|
68
|
+
when Prism::ProgramNode
|
|
69
|
+
walk(node.statements, namespace, contracts)
|
|
70
|
+
when Prism::StatementsNode
|
|
71
|
+
node.body.each { |stmt| walk(stmt, namespace, contracts) }
|
|
72
|
+
when Prism::ModuleNode
|
|
73
|
+
walk(node.body, namespace + [constant_name(node.constant_path)], contracts)
|
|
74
|
+
when Prism::ClassNode
|
|
75
|
+
class_name = (namespace + [constant_name(node.constant_path)]).compact.join("::")
|
|
76
|
+
if node.body.is_a?(Prism::StatementsNode)
|
|
77
|
+
node.body.body.each do |stmt|
|
|
78
|
+
if stmt.is_a?(Prism::CallNode) && stmt.receiver.nil? && stmt.name == :contract
|
|
79
|
+
contracts << parse_contract(stmt, class_name)
|
|
80
|
+
else
|
|
81
|
+
walk(stmt, namespace + [constant_name(node.constant_path)], contracts)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
else
|
|
85
|
+
walk(node.body, namespace + [constant_name(node.constant_path)], contracts)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def parse_contract(call, screen_name)
|
|
91
|
+
positional, kwargs = split_arguments(call)
|
|
92
|
+
if positional.length != 1 || !kwargs.empty?
|
|
93
|
+
fail_at!(call, "contract expects a single component name literal (e.g. contract \"ConversationView\" do |c| ... end)")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
component = literal_name(positional.first, call, "component name")
|
|
97
|
+
builder = Native::ContractBuilder.new(component, screen: screen_name)
|
|
98
|
+
|
|
99
|
+
if call.block
|
|
100
|
+
parse_contract_block(call.block, builder, allow_actions: true)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
builder.build
|
|
104
|
+
rescue ArgumentError => e
|
|
105
|
+
fail_at!(call, e.message)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def parse_contract_block(block, builder, allow_actions:)
|
|
109
|
+
fail_at!(block, "contract expects a `do |c| ... end` block") unless block.is_a?(Prism::BlockNode)
|
|
110
|
+
|
|
111
|
+
param_name = block_param_name(block)
|
|
112
|
+
return unless block.body
|
|
113
|
+
|
|
114
|
+
statements = block.body.is_a?(Prism::StatementsNode) ? block.body.body : [block.body]
|
|
115
|
+
statements.each do |stmt|
|
|
116
|
+
fail_at!(stmt, "Only declarative contract calls are allowed inside a contract block") unless stmt.is_a?(Prism::CallNode)
|
|
117
|
+
check_receiver!(stmt, param_name)
|
|
118
|
+
|
|
119
|
+
case stmt.name
|
|
120
|
+
when *SCALAR_METHODS
|
|
121
|
+
parse_scalar_field(stmt, stmt.name, builder)
|
|
122
|
+
when :prop
|
|
123
|
+
parse_prop_field(stmt, builder)
|
|
124
|
+
when :one
|
|
125
|
+
parse_one_field(stmt, builder)
|
|
126
|
+
when :many
|
|
127
|
+
parse_many_field(stmt, builder)
|
|
128
|
+
when :action
|
|
129
|
+
fail_at!(stmt, "Actions may only be declared at the top level of a contract") unless allow_actions
|
|
130
|
+
parse_action(stmt, builder)
|
|
131
|
+
else
|
|
132
|
+
fail_at!(stmt, "Unknown contract declaration #{stmt.name.inspect}")
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def parse_scalar_field(call, type, builder)
|
|
138
|
+
positional, kwargs = split_arguments(call)
|
|
139
|
+
fail_at!(call, "#{call.name} expects a field name") unless positional.length == 1
|
|
140
|
+
fail_at!(call, "#{call.name} does not take a block") if call.block
|
|
141
|
+
|
|
142
|
+
name = literal_name(positional.first, call, "field name")
|
|
143
|
+
optional = extract_optional!(kwargs, call, allowed: %i[optional])
|
|
144
|
+
builder.prop(name, type, optional: optional)
|
|
145
|
+
rescue ArgumentError => e
|
|
146
|
+
fail_at!(call, e.message)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def parse_prop_field(call, builder)
|
|
150
|
+
positional, kwargs = split_arguments(call)
|
|
151
|
+
unless positional.length == 1 || positional.length == 2
|
|
152
|
+
fail_at!(call, "prop expects (name, type = :string, optional: false)")
|
|
153
|
+
end
|
|
154
|
+
fail_at!(call, "prop does not take a block") if call.block
|
|
155
|
+
|
|
156
|
+
name = literal_name(positional[0], call, "field name")
|
|
157
|
+
type = positional[1] ? literal_name(positional[1], call, "field type") : "string"
|
|
158
|
+
optional = extract_optional!(kwargs, call, allowed: %i[optional])
|
|
159
|
+
builder.prop(name, type, optional: optional)
|
|
160
|
+
rescue ArgumentError => e
|
|
161
|
+
fail_at!(call, e.message)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def parse_one_field(call, builder)
|
|
165
|
+
positional, kwargs = split_arguments(call)
|
|
166
|
+
fail_at!(call, "one expects a field name and a block") unless positional.length == 1 && call.block
|
|
167
|
+
|
|
168
|
+
name = literal_name(positional.first, call, "field name")
|
|
169
|
+
optional = extract_optional!(kwargs, call, allowed: %i[optional as])
|
|
170
|
+
as = kwargs.key?(:as) ? literal_name(kwargs[:as], call, "as:") : nil
|
|
171
|
+
|
|
172
|
+
builder.one(name, as: as, optional: optional) do |nested|
|
|
173
|
+
parse_contract_block(call.block, nested, allow_actions: false)
|
|
174
|
+
end
|
|
175
|
+
rescue ArgumentError => e
|
|
176
|
+
fail_at!(call, e.message)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def parse_many_field(call, builder)
|
|
180
|
+
positional, kwargs = split_arguments(call)
|
|
181
|
+
fail_at!(call, "many expects a field name") unless positional.length == 1
|
|
182
|
+
|
|
183
|
+
name = literal_name(positional.first, call, "field name")
|
|
184
|
+
optional = extract_optional!(kwargs, call, allowed: %i[optional as of])
|
|
185
|
+
as = kwargs.key?(:as) ? literal_name(kwargs[:as], call, "as:") : nil
|
|
186
|
+
of = kwargs.key?(:of) ? literal_name(kwargs[:of], call, "of:") : nil
|
|
187
|
+
|
|
188
|
+
if of
|
|
189
|
+
fail_at!(call, "many #{name.inspect} with of: cannot also take a block") if call.block
|
|
190
|
+
fail_at!(call, "many #{name.inspect} with of: cannot also specify as:") if as
|
|
191
|
+
builder.many(name, of: of, optional: optional)
|
|
192
|
+
else
|
|
193
|
+
fail_at!(call, "many #{name.inspect} requires either of: or a block") unless call.block
|
|
194
|
+
builder.many(name, as: as, optional: optional) do |nested|
|
|
195
|
+
parse_contract_block(call.block, nested, allow_actions: false)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
rescue ArgumentError => e
|
|
199
|
+
fail_at!(call, e.message)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def parse_action(call, builder)
|
|
203
|
+
positional, kwargs = split_arguments(call)
|
|
204
|
+
fail_at!(call, "action expects a single action name") unless positional.length == 1 && kwargs.empty?
|
|
205
|
+
|
|
206
|
+
name = literal_name(positional.first, call, "action name")
|
|
207
|
+
if call.block
|
|
208
|
+
builder.action(name) { |action_builder| parse_action_block(call.block, action_builder) }
|
|
209
|
+
else
|
|
210
|
+
builder.action(name)
|
|
211
|
+
end
|
|
212
|
+
rescue ArgumentError => e
|
|
213
|
+
fail_at!(call, e.message)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def parse_action_block(block, action_builder)
|
|
217
|
+
fail_at!(block, "action expects a `do |a| ... end` block") unless block.is_a?(Prism::BlockNode)
|
|
218
|
+
|
|
219
|
+
param_name = block_param_name(block)
|
|
220
|
+
return unless block.body
|
|
221
|
+
|
|
222
|
+
statements = block.body.is_a?(Prism::StatementsNode) ? block.body.body : [block.body]
|
|
223
|
+
statements.each do |stmt|
|
|
224
|
+
fail_at!(stmt, "Only arg/value declarations are allowed inside an action block") unless stmt.is_a?(Prism::CallNode)
|
|
225
|
+
check_receiver!(stmt, param_name)
|
|
226
|
+
fail_at!(stmt, "Unknown action parameter declaration #{stmt.name.inspect} (expected arg or value)") unless %i[arg value].include?(stmt.name)
|
|
227
|
+
fail_at!(stmt, "#{stmt.name} does not take a block") if stmt.block
|
|
228
|
+
|
|
229
|
+
positional, kwargs = split_arguments(stmt)
|
|
230
|
+
unless positional.length == 1 || positional.length == 2
|
|
231
|
+
fail_at!(stmt, "#{stmt.name} expects (name, type = :string, optional: false)")
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
name = literal_name(positional[0], stmt, "parameter name")
|
|
235
|
+
type = positional[1] ? literal_name(positional[1], stmt, "parameter type") : "string"
|
|
236
|
+
optional = extract_optional!(kwargs, stmt, allowed: %i[optional])
|
|
237
|
+
|
|
238
|
+
if stmt.name == :arg
|
|
239
|
+
action_builder.arg(name, type, optional: optional)
|
|
240
|
+
else
|
|
241
|
+
action_builder.value(name, type, optional: optional)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
rescue ArgumentError => e
|
|
245
|
+
fail_at!(block, e.message)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def block_param_name(block)
|
|
249
|
+
params = block.parameters
|
|
250
|
+
return nil unless params.is_a?(Prism::BlockParametersNode) && params.parameters
|
|
251
|
+
|
|
252
|
+
requireds = params.parameters.requireds
|
|
253
|
+
return nil if requireds.empty?
|
|
254
|
+
|
|
255
|
+
if requireds.length != 1 || !requireds.first.is_a?(Prism::RequiredParameterNode)
|
|
256
|
+
fail_at!(block, "Contract blocks expect a single builder parameter (e.g. do |c|)")
|
|
257
|
+
end
|
|
258
|
+
requireds.first.name
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def check_receiver!(call, param_name)
|
|
262
|
+
if param_name
|
|
263
|
+
unless call.receiver.is_a?(Prism::LocalVariableReadNode) && call.receiver.name == param_name
|
|
264
|
+
fail_at!(call, "Call #{call.name} on the block builder `#{param_name}`")
|
|
265
|
+
end
|
|
266
|
+
elsif call.receiver
|
|
267
|
+
fail_at!(call, "Unexpected receiver on #{call.name}")
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def split_arguments(call)
|
|
272
|
+
return [[], {}] unless call.arguments
|
|
273
|
+
|
|
274
|
+
positional = []
|
|
275
|
+
kwargs = {}
|
|
276
|
+
call.arguments.arguments.each do |arg|
|
|
277
|
+
if arg.is_a?(Prism::KeywordHashNode)
|
|
278
|
+
arg.elements.each do |element|
|
|
279
|
+
unless element.is_a?(Prism::AssocNode) && element.key.is_a?(Prism::SymbolNode)
|
|
280
|
+
fail_at!(element, "Contract keyword arguments must use literal symbol keys")
|
|
281
|
+
end
|
|
282
|
+
kwargs[element.key.unescaped.to_sym] = element.value
|
|
283
|
+
end
|
|
284
|
+
else
|
|
285
|
+
positional << arg
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
[positional, kwargs]
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def extract_optional!(kwargs, call, allowed:)
|
|
292
|
+
unknown = kwargs.keys - allowed
|
|
293
|
+
unless unknown.empty?
|
|
294
|
+
fail_at!(call, "Unknown keyword argument(s) #{unknown.map(&:inspect).join(', ')} on #{call.name}")
|
|
295
|
+
end
|
|
296
|
+
return false unless kwargs.key?(:optional)
|
|
297
|
+
|
|
298
|
+
literal_bool(kwargs[:optional], call, "optional:")
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def literal_name(node, context, label)
|
|
302
|
+
case node
|
|
303
|
+
when Prism::StringNode
|
|
304
|
+
node.unescaped
|
|
305
|
+
when Prism::SymbolNode
|
|
306
|
+
node.unescaped
|
|
307
|
+
else
|
|
308
|
+
fail_at!(context, "#{label} must be a string or symbol literal")
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def literal_bool(node, context, label)
|
|
313
|
+
case node
|
|
314
|
+
when Prism::TrueNode then true
|
|
315
|
+
when Prism::FalseNode then false
|
|
316
|
+
else
|
|
317
|
+
fail_at!(context, "#{label} must be true or false")
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def constant_name(node)
|
|
322
|
+
case node
|
|
323
|
+
when Prism::ConstantReadNode
|
|
324
|
+
node.name.to_s
|
|
325
|
+
when Prism::ConstantPathNode
|
|
326
|
+
parent = node.parent ? constant_name(node.parent) : nil
|
|
327
|
+
[parent, node.name.to_s].compact.join("::")
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def fail_at!(node, message)
|
|
332
|
+
line = node.location.start_line
|
|
333
|
+
raise BuildError, "#{@path}:#{line}: #{message}"
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require_relative "runner"
|
|
2
|
+
|
|
3
|
+
module InstantRecord
|
|
4
|
+
class Compiler
|
|
5
|
+
# Replaces a build machine's absolute path prefix inside a compiled Wasm
|
|
6
|
+
# binary with a neutral prefix of identical byte length so data-segment
|
|
7
|
+
# offsets stay intact.
|
|
8
|
+
module PathScrubber
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def scrub(path, prefix)
|
|
12
|
+
prefix_bytes = prefix.to_s.b
|
|
13
|
+
raise ArgumentError, "prefix must not be empty" if prefix_bytes.empty?
|
|
14
|
+
|
|
15
|
+
data = File.binread(path)
|
|
16
|
+
neutral = ("/build" + "/_" * prefix_bytes.bytesize).byteslice(0, prefix_bytes.bytesize).b
|
|
17
|
+
raise BuildError, "neutral prefix length mismatch" unless neutral.bytesize == prefix_bytes.bytesize
|
|
18
|
+
|
|
19
|
+
count = 0
|
|
20
|
+
patched = data.gsub(prefix_bytes) do
|
|
21
|
+
count += 1
|
|
22
|
+
neutral
|
|
23
|
+
end
|
|
24
|
+
raise BuildError, "scrubbed wasm size changed" unless patched.bytesize == data.bytesize
|
|
25
|
+
|
|
26
|
+
File.binwrite(path, patched)
|
|
27
|
+
{ count: count, remaining: patched.include?(prefix_bytes) ? patched.scan(prefix_bytes).length : 0, size: patched.bytesize }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|