runtime_analysis 1.0.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 +7 -0
- data/CHANGELOG.md +115 -0
- data/LICENSE.txt +96 -0
- data/README.md +164 -0
- data/editor/nvim/README.md +278 -0
- data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
- data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
- data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
- data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
- data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
- data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
- data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
- data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
- data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
- data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
- data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
- data/editor/nvim/plugin/runtime_analysis.lua +71 -0
- data/examples/trackers/PROMPT.md +86 -0
- data/examples/trackers/honeybadger.rb +19 -0
- data/examples/trackers/sentry.rb +16 -0
- data/ext/runtime_analysis/extconf.rb +68 -0
- data/ext/runtime_analysis/fast.c +775 -0
- data/ext/runtime_analysis/fast.h +59 -0
- data/ext/runtime_analysis/hold.c +560 -0
- data/ext/runtime_analysis/init.c +36 -0
- data/ext/runtime_analysis/names.c +78 -0
- data/ext/runtime_analysis/names.h +28 -0
- data/ext/runtime_analysis/novelty.c +139 -0
- data/ext/runtime_analysis/novelty.h +22 -0
- data/ext/runtime_analysis/os.c +73 -0
- data/ext/runtime_analysis/os.h +32 -0
- data/ext/runtime_analysis/ring.c +445 -0
- data/ext/runtime_analysis/rows.c +304 -0
- data/ext/runtime_analysis/sink.c +485 -0
- data/ext/runtime_analysis/sink.h +240 -0
- data/ext/runtime_analysis/state.c +254 -0
- data/ext/runtime_analysis/state.h +57 -0
- data/ext/runtime_analysis/wrapper.c +278 -0
- data/ext/runtime_analysis/wrapper.h +24 -0
- data/lib/runtime_analysis/errors.rb +34 -0
- data/lib/runtime_analysis/failsafe.rb +123 -0
- data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
- data/lib/runtime_analysis/host/declared.rb +152 -0
- data/lib/runtime_analysis/host/inject.rb +90 -0
- data/lib/runtime_analysis/host/job_runners.rb +27 -0
- data/lib/runtime_analysis/host/middleware.rb +23 -0
- data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
- data/lib/runtime_analysis/host/railtie.rb +42 -0
- data/lib/runtime_analysis/host/reports.rb +54 -0
- data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
- data/lib/runtime_analysis/host/web.rb +22 -0
- data/lib/runtime_analysis/host.rb +34 -0
- data/lib/runtime_analysis/inject.rb +5 -0
- data/lib/runtime_analysis/minitest.rb +9 -0
- data/lib/runtime_analysis/native/build.rb +17 -0
- data/lib/runtime_analysis/native/byte_size.rb +27 -0
- data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
- data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
- data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
- data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
- data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
- data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
- data/lib/runtime_analysis/native/sink.rb +12 -0
- data/lib/runtime_analysis/native.rb +74 -0
- data/lib/runtime_analysis/platform.rb +30 -0
- data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
- data/lib/runtime_analysis/query/index/graph.rb +107 -0
- data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
- data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
- data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
- data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
- data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
- data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
- data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
- data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
- data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
- data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
- data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
- data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
- data/lib/runtime_analysis/query/index/reading.rb +80 -0
- data/lib/runtime_analysis/query/index/shapes.rb +142 -0
- data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
- data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
- data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
- data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
- data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
- data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
- data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
- data/lib/runtime_analysis/query/index/signatures.rb +93 -0
- data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
- data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
- data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
- data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
- data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
- data/lib/runtime_analysis/query/index/static/method.rb +19 -0
- data/lib/runtime_analysis/query/index/static/params.rb +52 -0
- data/lib/runtime_analysis/query/index/static/source.rb +69 -0
- data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
- data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
- data/lib/runtime_analysis/query/index/static/value.rb +17 -0
- data/lib/runtime_analysis/query/index/static/values.rb +71 -0
- data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
- data/lib/runtime_analysis/query/index/static.rb +85 -0
- data/lib/runtime_analysis/query/index.rb +32 -0
- data/lib/runtime_analysis/query/nodes.rb +60 -0
- data/lib/runtime_analysis/query/project/config.rb +108 -0
- data/lib/runtime_analysis/query/project.rb +40 -0
- data/lib/runtime_analysis/query/report/document.rb +84 -0
- data/lib/runtime_analysis/query/report/finding.rb +15 -0
- data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
- data/lib/runtime_analysis/query/report/list.rb +15 -0
- data/lib/runtime_analysis/query/report/location.rb +14 -0
- data/lib/runtime_analysis/query/report/one_row.rb +14 -0
- data/lib/runtime_analysis/query/report/outline.rb +15 -0
- data/lib/runtime_analysis/query/report/paint.rb +34 -0
- data/lib/runtime_analysis/query/report/related.rb +11 -0
- data/lib/runtime_analysis/query/report/table.rb +15 -0
- data/lib/runtime_analysis/query/report/text.rb +13 -0
- data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
- data/lib/runtime_analysis/query/report.rb +35 -0
- data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
- data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
- data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
- data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
- data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
- data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
- data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
- data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
- data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
- data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
- data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
- data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
- data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
- data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
- data/lib/runtime_analysis/query/surfaces.rb +14 -0
- data/lib/runtime_analysis/query/tools/base.rb +86 -0
- data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
- data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
- data/lib/runtime_analysis/query/tools/origins.rb +47 -0
- data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
- data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
- data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
- data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
- data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
- data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
- data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
- data/lib/runtime_analysis/query/tools.rb +34 -0
- data/lib/runtime_analysis/query.rb +33 -0
- data/lib/runtime_analysis/recording/blob.rb +13 -0
- data/lib/runtime_analysis/recording/format.rb +32 -0
- data/lib/runtime_analysis/recording/opcodes.rb +30 -0
- data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
- data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
- data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
- data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
- data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
- data/lib/runtime_analysis/recording/seen.rb +63 -0
- data/lib/runtime_analysis/recording/sink.rb +19 -0
- data/lib/runtime_analysis/recording/source_blob.rb +20 -0
- data/lib/runtime_analysis/recording.rb +52 -0
- data/lib/runtime_analysis/rspec.rb +8 -0
- data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
- data/lib/runtime_analysis/ruby_parsing.rb +98 -0
- data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
- data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
- data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
- data/lib/runtime_analysis/trace/configuration.rb +115 -0
- data/lib/runtime_analysis/trace/entries.rb +47 -0
- data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
- data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
- data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
- data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
- data/lib/runtime_analysis/trace/hold.rb +142 -0
- data/lib/runtime_analysis/trace/inclusion.rb +46 -0
- data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
- data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
- data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
- data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
- data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
- data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
- data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
- data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
- data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
- data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
- data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
- data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
- data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
- data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
- data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
- data/lib/runtime_analysis/trace/installer.rb +149 -0
- data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
- data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
- data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
- data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
- data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
- data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
- data/lib/runtime_analysis/trace/mechanism.rb +28 -0
- data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
- data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
- data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
- data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
- data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
- data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
- data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
- data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
- data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
- data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
- data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
- data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
- data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
- data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
- data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
- data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
- data/lib/runtime_analysis/trace/novelty.rb +17 -0
- data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
- data/lib/runtime_analysis/trace/pure/state.rb +94 -0
- data/lib/runtime_analysis/trace/pure.rb +15 -0
- data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
- data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
- data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
- data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
- data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
- data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
- data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
- data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
- data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
- data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
- data/lib/runtime_analysis/trace/recorder.rb +149 -0
- data/lib/runtime_analysis/trace/state.rb +16 -0
- data/lib/runtime_analysis/trace.rb +142 -0
- data/lib/runtime_analysis/version.rb +6 -0
- data/lib/runtime_analysis/web.rb +6 -0
- data/lib/runtime_analysis.rb +125 -0
- metadata +330 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through trace_spec.rb and installer_spec.rb, which define methods and call them.
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Trace
|
|
7
|
+
class Installer
|
|
8
|
+
module Wrapping
|
|
9
|
+
autoload :IvarWrites, File.join(__dir__, "wrapping/ivar_writes")
|
|
10
|
+
|
|
11
|
+
KEYWORDS = %i[key keyreq keyrest].freeze
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def watch(capture)
|
|
16
|
+
@probe = Probe.new(capture.probe)
|
|
17
|
+
@ivar_writes = IvarWrites.new if capture.ivars
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def wrap_traced(target, name, class_method, method, reach)
|
|
21
|
+
remember(target, name, method)
|
|
22
|
+
id = register(target, name, class_method, method)
|
|
23
|
+
@registry.limit(id, reach) unless reach == :app
|
|
24
|
+
tell_wrapper(id, name, reach, method)
|
|
25
|
+
wrap(target, name, id)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def wrap(target, name, id)
|
|
29
|
+
@wrapper.wrap(target, name, id: id, probed: @registry.probed?(id))
|
|
30
|
+
@wrapped.add([target, name])
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def tell_wrapper(id, name, reach, method)
|
|
34
|
+
entry = @registry.fetch(id)
|
|
35
|
+
@registry.salt(id, entry.hash)
|
|
36
|
+
tell_extension(id, reach == :app && !name.equal?(:method_missing) && !@registry.probed?(id), entry)
|
|
37
|
+
watch_ivars(id, method) if @ivar_writes
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
if Native.loaded?
|
|
41
|
+
WRAPPER = Native.wrapper
|
|
42
|
+
|
|
43
|
+
def tell_extension(id, fast, entry)
|
|
44
|
+
WRAPPER.register(id, fast, entry.fetch(6), entry.fetch(5), entry.fetch(7))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def watch_ivars(id, method)
|
|
48
|
+
file, line = method.source_location
|
|
49
|
+
WRAPPER.watch_ivars(id, @ivar_writes.of(file, line))
|
|
50
|
+
end
|
|
51
|
+
else
|
|
52
|
+
def tell_extension(_id, _fast, _entry) = nil
|
|
53
|
+
def watch_ivars(_id, _method) = nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def register(target, name, class_method, method)
|
|
57
|
+
file, line = location_of(target, name, method)
|
|
58
|
+
id = @registry.register(target, name, class_method: class_method, file: file, line: line)
|
|
59
|
+
@registry.keywords_as_hash(id) unless method.parameters.any? { |kind, _| KEYWORDS.include?(kind) }
|
|
60
|
+
@registry.probe(id) if @probe.covers?(target, name, class_method)
|
|
61
|
+
id
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "installer/registry"
|
|
4
|
+
require_relative "installer/probe"
|
|
5
|
+
require_relative "inclusion"
|
|
6
|
+
require_relative "installer/alias_repair"
|
|
7
|
+
require_relative "installer/reflection"
|
|
8
|
+
require_relative "installer/hook"
|
|
9
|
+
require_relative "installer/deferral"
|
|
10
|
+
require_relative "installer/included_gems"
|
|
11
|
+
require_relative "installer/built_classes"
|
|
12
|
+
require_relative "installer/wrapping"
|
|
13
|
+
require_relative "installer/sorbet"
|
|
14
|
+
require_relative "installer/answers"
|
|
15
|
+
|
|
16
|
+
module RuntimeAnalysis
|
|
17
|
+
module Trace
|
|
18
|
+
class Installer
|
|
19
|
+
NEVER_WRAPPED = [::BasicObject, ::Object, ::Kernel, ::Module, ::Class].freeze
|
|
20
|
+
|
|
21
|
+
autoload :CalledNames, File.join(__dir__, "installer/called_names")
|
|
22
|
+
|
|
23
|
+
include AliasRepair
|
|
24
|
+
include Deferral
|
|
25
|
+
include IncludedGems
|
|
26
|
+
include Hook.unheard
|
|
27
|
+
include BuiltClasses
|
|
28
|
+
include Wrapping
|
|
29
|
+
extend Answers
|
|
30
|
+
|
|
31
|
+
def self.copying
|
|
32
|
+
was = copying?
|
|
33
|
+
Thread.current[:runtime_analysis_copying] = true
|
|
34
|
+
yield
|
|
35
|
+
ensure
|
|
36
|
+
Thread.current[:runtime_analysis_copying] = was || false
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.copying? = Thread.current[:runtime_analysis_copying]
|
|
40
|
+
|
|
41
|
+
def initialize(registry:, inclusion:, wrapper:, called: nil, capture: Capture.new)
|
|
42
|
+
@registry = registry
|
|
43
|
+
@inclusion = inclusion
|
|
44
|
+
@wrapper = wrapper
|
|
45
|
+
init_tracking(called, capture)
|
|
46
|
+
init_deferral
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def install
|
|
50
|
+
Native.require! if @wrapper.needs_extension?
|
|
51
|
+
Module.prepend(Hook)
|
|
52
|
+
Reflection.install
|
|
53
|
+
Sorbet.install
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def wraps?(mod) = @wrapper.own?(mod)
|
|
57
|
+
|
|
58
|
+
def last_original(wrapper_module, name)
|
|
59
|
+
@remembered.fetch([@wrapper.target_of(wrapper_module), name])
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def own_beneath(wrapper_method)
|
|
63
|
+
beneath = wrapper_method.super_method
|
|
64
|
+
beneath if beneath && beneath.owner == @wrapper.target_of(wrapper_method.owner)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def on_defined(mod, name, class_method:)
|
|
68
|
+
target = class_method ? mod.singleton_class : mod
|
|
69
|
+
return if untouchable?(mod, target)
|
|
70
|
+
|
|
71
|
+
Failsafe.attempt { @paused ? enqueue(target, name, class_method) : discover(target, name, class_method) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def untouchable?(mod, target)
|
|
75
|
+
self.class.copying? || NEVER_WRAPPED.include?(mod) || !keyable?(target) || one_object?(target)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def on_removed(mod, name, class_method:)
|
|
79
|
+
key = [class_method ? mod.singleton_class : mod, name]
|
|
80
|
+
return unless ask_host(false) { @wrapped.include?(key) }
|
|
81
|
+
|
|
82
|
+
Failsafe.attempt { forget(*key) }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def resync(mod)
|
|
86
|
+
[mod, mod.singleton_class].each do |target|
|
|
87
|
+
Failsafe.attempt { @wrapper.resync_visibility(target) } if ask_host(false) { @wrapper.wrapped?(target) }
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def ask_host(otherwise)
|
|
94
|
+
yield
|
|
95
|
+
rescue StandardError
|
|
96
|
+
otherwise
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def keyable?(target) = ask_host(false) { target.hash }
|
|
100
|
+
|
|
101
|
+
def forget(target, name)
|
|
102
|
+
@wrapped.delete([target, name])
|
|
103
|
+
@originals.delete([target, name])
|
|
104
|
+
@wrapper.unwrap(target, name, beneath: @remembered.fetch([target, name]))
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def init_tracking(called, capture)
|
|
108
|
+
@called = called
|
|
109
|
+
watch(capture)
|
|
110
|
+
@wrapped = Set.new
|
|
111
|
+
@originals = {}
|
|
112
|
+
@remembered = {}
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def discover(target, name, class_method)
|
|
116
|
+
restore_displaced(target, name)
|
|
117
|
+
method = original(target, name)
|
|
118
|
+
return if settled?(target, name, method)
|
|
119
|
+
|
|
120
|
+
reach = reach_of(name, location_of(target, name, method)&.first)
|
|
121
|
+
reach ? wrap_traced(target, name, class_method, method, reach) : redefined(target, name, method)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def redefined(target, name, method)
|
|
125
|
+
remember(target, name, method) if @originals.key?([target, name])
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def remember(target, name, method)
|
|
129
|
+
@originals[[target, name]] = @remembered[[target, name]] = method
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def original(target, name)
|
|
133
|
+
Enumerator.produce(defined_on(target, name), &:super_method).find { |method| !wrapper?(method) }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def wrapper?(method)
|
|
137
|
+
method && @wrapper.own?(method.owner)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def defined_on(target, name)
|
|
141
|
+
raw_method(target, name)
|
|
142
|
+
rescue NameError
|
|
143
|
+
nil
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def raw_method(mod, name) = Reflection.raw_instance_method(mod, name)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through trace_spec.rb, pure_wrapper_spec.rb and the wrapper specs, which
|
|
4
|
+
# record calls through these.
|
|
5
|
+
|
|
6
|
+
module RuntimeAnalysis
|
|
7
|
+
module Trace
|
|
8
|
+
module Mechanism
|
|
9
|
+
module BlockEntries
|
|
10
|
+
def record(id, receiver, args, kwargs, &)
|
|
11
|
+
return yield unless enabled?
|
|
12
|
+
|
|
13
|
+
@recorder.record(id, receiver, args, kwargs, &)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def record_spread(id, receiver, *args, **kwargs, &)
|
|
17
|
+
return yield unless enabled?
|
|
18
|
+
|
|
19
|
+
@recorder.record(id, receiver, args, kwargs, &)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call_displaced(displaced, receiver, *args, **kwargs, &block)
|
|
23
|
+
id, beneath = displaced
|
|
24
|
+
record(id, receiver, args, kwargs) { beneath.bind_call(receiver, *args, **kwargs, &block) }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "runtime_analysis/ruby_parsing"
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Trace
|
|
7
|
+
module Mechanism
|
|
8
|
+
class FrameReaders
|
|
9
|
+
MENTION = /\b(?:caller|caller_locations|of_caller)\b|\buplevel:/
|
|
10
|
+
METHOD_LIKE = %i[def_node block_node lambda_node].freeze
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@mentions = {}
|
|
14
|
+
@methods = {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def reads_caller?(file, line)
|
|
18
|
+
return false unless file && line && mentions?(file)
|
|
19
|
+
|
|
20
|
+
!RubyParsing.available? || method_mentions?(file, line)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def method_mentions?(file, line)
|
|
26
|
+
node = smallest_method_at(file, line)
|
|
27
|
+
node ? MENTION.match?(node.slice) : false
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def mentions?(file)
|
|
31
|
+
@mentions.fetch(file) { @mentions[file] = File.file?(file) && MENTION.match?(File.read(file)) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def methods_in(file)
|
|
35
|
+
@methods.fetch(file) do
|
|
36
|
+
found = []
|
|
37
|
+
each_node(RubyParsing.tree_of_file(file)) { |node| found << node if method_like?(node) }
|
|
38
|
+
@methods[file] = found
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def smallest_method_at(file, line)
|
|
43
|
+
methods_in(file).reverse_each.find { |node| covers?(node, line) }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def each_node(node, &block)
|
|
47
|
+
yield node
|
|
48
|
+
node.compact_child_nodes.each { |child| each_node(child, &block) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def method_like?(node)
|
|
52
|
+
METHOD_LIKE.include?(node.type)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def covers?(node, line)
|
|
56
|
+
node.start_line <= line && line <= node.end_line
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through fast_path_spec.rb, which records each scenario through Ruby and
|
|
4
|
+
# through the wrapper and compares what was written.
|
|
5
|
+
|
|
6
|
+
require "runtime_analysis/trace/recorder"
|
|
7
|
+
|
|
8
|
+
module RuntimeAnalysis
|
|
9
|
+
module Trace
|
|
10
|
+
module Mechanism
|
|
11
|
+
module NativeAsks
|
|
12
|
+
CLASS_NAMES = Recorder.class_names
|
|
13
|
+
|
|
14
|
+
def class_id(handle, value)
|
|
15
|
+
Failsafe.attempt { @recorder.logger.name_id(handle, CLASS_NAMES.of_value(value)) }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def keyword_id(handle, name)
|
|
19
|
+
Failsafe.attempt { @recorder.logger.name_id(handle, name.to_s) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def record_outcome(token, error, result)
|
|
23
|
+
@recorder.outcome(token, error, result)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def raised_in(receiver, error)
|
|
27
|
+
Failsafe.attempt { hold&.keep_alive(receiver, error) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def tree_ended(error)
|
|
31
|
+
Failsafe.attempt { @recorder.hold&.tree_ended(State.current, error) }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through trace_spec.rb and fast_path_spec.rb, which start and stop the tracer.
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Trace
|
|
7
|
+
module Mechanism
|
|
8
|
+
module NativeSync
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
if Native.loaded?
|
|
12
|
+
WRAPPER = Native.wrapper
|
|
13
|
+
|
|
14
|
+
def start_wrapper(tier)
|
|
15
|
+
gate = @recorder.novelty
|
|
16
|
+
ours = gate.nil? || gate.instance_of?(Novelty)
|
|
17
|
+
WRAPPER.start(tier, (gate if ours), ours, !hold.nil?)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def stop_wrapper = WRAPPER.stop
|
|
21
|
+
else
|
|
22
|
+
def start_wrapper(_tier) = nil
|
|
23
|
+
def stop_wrapper = nil
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "ruby_wrapper"
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Trace
|
|
7
|
+
module Mechanism
|
|
8
|
+
class PureWrapper < RubyWrapper
|
|
9
|
+
TEMPLATE = <<~RUBY.freeze
|
|
10
|
+
def %<name>s(%<params>s)
|
|
11
|
+
#{DISPLACED_CHECK}
|
|
12
|
+
Trace.record_spread(%<id>d, self%<passes>s) { super }
|
|
13
|
+
end
|
|
14
|
+
RUBY
|
|
15
|
+
|
|
16
|
+
BLOCK_TEMPLATE = <<~RUBY
|
|
17
|
+
define_method(%<name>p) do |*__args, **__kwargs, &__block|
|
|
18
|
+
__beneath = DISPLACED[%<id>d]
|
|
19
|
+
next Trace.call_displaced([%<id>d, __beneath], self, *__args, **__kwargs, &__block) if __beneath
|
|
20
|
+
|
|
21
|
+
Trace.record(%<id>d, self, __args, __kwargs) { super(*__args, **__kwargs, &__block) }
|
|
22
|
+
end
|
|
23
|
+
RUBY
|
|
24
|
+
|
|
25
|
+
def needs_extension? = false
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def define_captured(mod, name, id, method, tier) = define_generated(mod, name, id, method, tier)
|
|
30
|
+
|
|
31
|
+
def source(name, id, method, tier)
|
|
32
|
+
format(TEMPLATE, name: name, id: id, **WrapperShape.for(name, method.parameters, tier: tier).to_h)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def by_block(name, id)
|
|
36
|
+
format(BLOCK_TEMPLATE, name: name, id: id)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "wrapper"
|
|
4
|
+
require "runtime_analysis/trace/installer"
|
|
5
|
+
require_relative "frame_readers"
|
|
6
|
+
require_relative "wrapper_shape"
|
|
7
|
+
require "runtime_analysis/trace/state"
|
|
8
|
+
|
|
9
|
+
module RuntimeAnalysis
|
|
10
|
+
module Trace
|
|
11
|
+
module Mechanism
|
|
12
|
+
class RubyWrapper < Wrapper
|
|
13
|
+
FILE = "(runtime_analysis wrapper)"
|
|
14
|
+
OPERATORS = %i[+ - * / % ** == != < <= > >= <=> << >> & | ^ ~ ! =~ !~ === [] []= +@ -@ ~@ !@].freeze
|
|
15
|
+
|
|
16
|
+
ENTRY = Native.wrapper
|
|
17
|
+
CAPTURING_FROM = 3
|
|
18
|
+
|
|
19
|
+
# rubocop:disable Style/MutableConstant -- hot path: a constant for each wrapper has YJIT recompile them all
|
|
20
|
+
DISPLACED = {}
|
|
21
|
+
# rubocop:enable Style/MutableConstant
|
|
22
|
+
DISPLACED_CHECK = <<~RUBY.chomp
|
|
23
|
+
unless DISPLACED.empty?
|
|
24
|
+
__beneath = DISPLACED[%<id>d]
|
|
25
|
+
return Trace.call_displaced([%<id>d, __beneath], self, %<forward>s) if __beneath
|
|
26
|
+
end
|
|
27
|
+
RUBY
|
|
28
|
+
|
|
29
|
+
PLAIN = <<~RUBY.freeze
|
|
30
|
+
def %<name>s(%<params>s)
|
|
31
|
+
#{DISPLACED_CHECK}
|
|
32
|
+
__ra = ENTRY.enter(%<id>d, self%<passes>s)
|
|
33
|
+
super
|
|
34
|
+
ensure
|
|
35
|
+
ENTRY.leave(__ra) if __ra
|
|
36
|
+
end
|
|
37
|
+
RUBY
|
|
38
|
+
|
|
39
|
+
CAPTURING = <<~RUBY.freeze
|
|
40
|
+
def %<name>s(%<params>s)
|
|
41
|
+
#{DISPLACED_CHECK}
|
|
42
|
+
__ra = ENTRY.enter(%<id>d, self%<passes>s)
|
|
43
|
+
__rv = super
|
|
44
|
+
__done = true
|
|
45
|
+
__rv
|
|
46
|
+
rescue ::Exception => __e
|
|
47
|
+
__done = true
|
|
48
|
+
Trace.raised_in(self, __e)
|
|
49
|
+
ENTRY.pass_on(__e)
|
|
50
|
+
ensure
|
|
51
|
+
ENTRY.leave(__ra, __rv, __e, __done) if __ra
|
|
52
|
+
end
|
|
53
|
+
RUBY
|
|
54
|
+
|
|
55
|
+
BY_BLOCK = <<~RUBY
|
|
56
|
+
define_method(%<name>p) do |*__args, **__kwargs, &__block|
|
|
57
|
+
__beneath = DISPLACED[%<id>d]
|
|
58
|
+
next Trace.call_displaced([%<id>d, __beneath], self, *__args, **__kwargs, &__block) if __beneath
|
|
59
|
+
|
|
60
|
+
__ra = ENTRY.enter(%<id>d, self, *__args, **__kwargs)
|
|
61
|
+
begin
|
|
62
|
+
__rv = super(*__args, **__kwargs, &__block)
|
|
63
|
+
__done = true
|
|
64
|
+
__rv
|
|
65
|
+
rescue ::Exception => __e
|
|
66
|
+
__done = true
|
|
67
|
+
Trace.raised_in(self, __e)
|
|
68
|
+
ENTRY.pass_on(__e)
|
|
69
|
+
ensure
|
|
70
|
+
ENTRY.leave(__ra, __rv, __e, __done) if __ra
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
RUBY
|
|
74
|
+
|
|
75
|
+
def initialize(tier:, frame_readers: FrameReaders.new)
|
|
76
|
+
super()
|
|
77
|
+
@tier = tier
|
|
78
|
+
@frame_readers = frame_readers
|
|
79
|
+
forget
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def forget
|
|
85
|
+
@native = Set.new
|
|
86
|
+
@ids = {}
|
|
87
|
+
DISPLACED.clear
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def define(mod, name, id, probed)
|
|
91
|
+
tier = probed ? CAPTURING_FROM : @tier
|
|
92
|
+
State.current.capture { define_captured(mod, name, id, method_of(target_of(mod), name), tier) }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def define_captured(mod, name, id, method, tier)
|
|
96
|
+
reads_caller?(method) ? define_natively(mod, name, id) : define_generated(mod, name, id, method, tier)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def define_generated(mod, name, id, method, tier)
|
|
100
|
+
@native.delete([mod, name])
|
|
101
|
+
DISPLACED.delete(@ids[[mod, name]])
|
|
102
|
+
define_in_ruby(mod, name, @ids[[mod, name]] = id, method, tier)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def define_in_ruby(mod, name, id, method, tier)
|
|
106
|
+
discard(mod, name)
|
|
107
|
+
mod.module_eval(definable?(name) ? source(name, id, method, tier) : by_block(name, id), FILE)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def discard(mod, name)
|
|
111
|
+
mod.remove_method(name) if mod.method_defined?(name) || mod.private_method_defined?(name)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def define_natively(mod, name, id)
|
|
115
|
+
@native.add([mod, name])
|
|
116
|
+
Wrapper.instance_method(:define).bind_call(self, mod, name, id)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def drop(mod, name, beneath)
|
|
120
|
+
@native.include?([mod, name]) ? super : DISPLACED[@ids.fetch([mod, name])] = beneath
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def reads_caller?(method)
|
|
124
|
+
file, line = Installer.source_location(method)
|
|
125
|
+
@frame_readers.reads_caller?(file, line)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def definable?(name) = name.match?(/\A[A-Za-z_][A-Za-z0-9_]*[?!=]?\z/) || OPERATORS.include?(name)
|
|
129
|
+
|
|
130
|
+
def method_of(target, name) = Installer.unpatched_method(target, name)
|
|
131
|
+
|
|
132
|
+
def source(name, id, method, tier)
|
|
133
|
+
shape = WrapperShape.for(name, method.parameters, tier: tier)
|
|
134
|
+
format(tier >= CAPTURING_FROM ? CAPTURING : PLAIN, name: name, id: id, **shape.to_h)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def by_block(name, id) = format(BY_BLOCK, name: name, id: id)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "runtime_analysis/native"
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Trace
|
|
7
|
+
module Mechanism
|
|
8
|
+
class Wrapper
|
|
9
|
+
def initialize
|
|
10
|
+
@modules = {}
|
|
11
|
+
@owned = {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def wrap(target, name, id:, probed: false)
|
|
15
|
+
visibility = own_visibility(target, name)
|
|
16
|
+
mod = module_for(target)
|
|
17
|
+
define(mod, name, id, probed)
|
|
18
|
+
mod.__send__(visibility, name)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def unwrap(target, name, beneath:)
|
|
22
|
+
mod = @modules.fetch(target)
|
|
23
|
+
drop(mod, name, beneath) if beneath
|
|
24
|
+
mod.remove_method(name)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def target_of(mod)
|
|
28
|
+
@owned.fetch(mod)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def needs_extension? = true
|
|
32
|
+
|
|
33
|
+
def own?(mod)
|
|
34
|
+
@owned.key?(mod)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def wrapped?(target)
|
|
38
|
+
@modules.key?(target)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def resync_visibility(target)
|
|
42
|
+
mod = @modules.fetch(target)
|
|
43
|
+
wrapped_names(mod).each { |name| mod.__send__(own_visibility(target, name), name) }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def wrapped_names(mod)
|
|
49
|
+
mod.instance_methods + mod.private_instance_methods
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def module_for(target)
|
|
53
|
+
@modules[target] ||= prepend_new(target)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def prepend_new(target)
|
|
57
|
+
Module.new.tap do |mod|
|
|
58
|
+
target.prepend(mod)
|
|
59
|
+
@owned[mod] = target
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def define(mod, name, id, *)
|
|
64
|
+
Native.wrapper.install(mod, name, id)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def drop(mod, name, beneath)
|
|
68
|
+
Native.wrapper.displace(mod, name, beneath)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def own_visibility(target, name)
|
|
72
|
+
return :private if target.private_method_defined?(name, false)
|
|
73
|
+
|
|
74
|
+
target.protected_method_defined?(name, false) ? :protected : :public
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuntimeAnalysis
|
|
4
|
+
module Trace
|
|
5
|
+
module Mechanism
|
|
6
|
+
WrapperShape = Struct.new(:params, :forward, :passes)
|
|
7
|
+
|
|
8
|
+
class WrapperShape
|
|
9
|
+
COPIED = %i[req keyreq block].freeze
|
|
10
|
+
FORWARD = new("...", "...", "")
|
|
11
|
+
SPLAT = new("*__args, **__kwargs, &", "*__args, **__kwargs, &", ", *__args, **__kwargs")
|
|
12
|
+
|
|
13
|
+
def self.for(name, parameters, tier:)
|
|
14
|
+
return SPLAT if name == :method_missing
|
|
15
|
+
return exact(parameters, tier) if copyable?(parameters)
|
|
16
|
+
|
|
17
|
+
tier >= 2 ? SPLAT : FORWARD
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.copyable?(parameters)
|
|
21
|
+
parameters && named_once?(parameters.map { |_, pname| pname }) &&
|
|
22
|
+
parameters.all? { |kind, _| COPIED.include?(kind) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.named_once?(names) = names.all? && names == names.uniq
|
|
26
|
+
|
|
27
|
+
def self.exact(parameters, tier)
|
|
28
|
+
positional = names_of(parameters, :req)
|
|
29
|
+
keywords = names_of(parameters, :keyreq)
|
|
30
|
+
passed = positional + keywords.map { |pname| "#{pname}: #{pname}" }
|
|
31
|
+
new(signature(positional, keywords), [*passed, "&"].join(", "), tier >= 2 ? passes(passed) : "")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.names_of(parameters, kind) = parameters.filter_map { |each, pname| pname if each == kind }
|
|
35
|
+
def self.signature(positional, keywords) = [*positional, *keywords.map { |pname| "#{pname}:" }, "&"].join(", ")
|
|
36
|
+
def self.passes(passed) = passed.map { |value| ", #{value}" }.join
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|