ruby-anything 0.0.0-stage → 0.1.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.
- package/.claude-plugin/marketplace.json +12 -0
- package/.claude-plugin/plugin.json +7 -0
- package/.mcp.json +8 -0
- package/LICENSE +21 -0
- package/README.md +82 -2
- package/adapters/node/harness.mjs +324 -0
- package/adapters/node/package-lock.json +26 -0
- package/adapters/node/package.json +7 -0
- package/adapters/polyglot/harness.py +350 -0
- package/adapters/polyglot/lang_csharp.py +222 -0
- package/adapters/polyglot/lang_go.py +283 -0
- package/adapters/polyglot/lang_java.py +510 -0
- package/adapters/polyglot/lang_kotlin.py +133 -0
- package/adapters/polyglot/lang_php.py +156 -0
- package/adapters/polyglot/lang_ruby.py +148 -0
- package/adapters/polyglot/lang_rust.py +430 -0
- package/adapters/polyglot/lang_swift.py +337 -0
- package/adapters/python/harness.py +417 -0
- package/bin/ruby-anything.js +10 -0
- package/docs/assets/how-it-works-dark.svg +51 -0
- package/docs/assets/how-it-works-light.svg +51 -0
- package/harness/ruby_replay.rb +144 -0
- package/package.json +51 -4
- package/skills/ruby-anything/SKILL.md +130 -0
- package/skills/ruby-anything/frontend.md +64 -0
- package/skills/ruby-anything/languages/csharp.md +43 -0
- package/skills/ruby-anything/languages/go.md +36 -0
- package/skills/ruby-anything/languages/java.md +54 -0
- package/skills/ruby-anything/languages/javascript.md +50 -0
- package/skills/ruby-anything/languages/kotlin.md +27 -0
- package/skills/ruby-anything/languages/php.md +47 -0
- package/skills/ruby-anything/languages/python.md +49 -0
- package/skills/ruby-anything/languages/rust.md +31 -0
- package/skills/ruby-anything/languages/swift.md +27 -0
- package/skills/ruby-anything/whole-app.md +168 -0
- package/src/adapters.js +60 -0
- package/src/analyze.js +183 -0
- package/src/app.js +697 -0
- package/src/cli.js +75 -0
- package/src/detect.js +266 -0
- package/src/go.js +139 -0
- package/src/mcp.js +89 -0
- package/src/setup.js +106 -0
- package/src/units.js +444 -0
- package/src/util.js +312 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""PHP support for the polyglot adapter.
|
|
2
|
+
|
|
3
|
+
Analysis: top-level `function` declarations (methods inside classes are skipped).
|
|
4
|
+
Driver: nothing compiles; prepare writes pp_driver.php, which requires the target file, decodes JSON args
|
|
5
|
+
(with $float tags), calls the function, maps thrown Throwables to raise outcomes and encodes the result
|
|
6
|
+
(floats stay floats via json_encode's serialize_precision=-1).
|
|
7
|
+
"""
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
11
|
+
import subprocess
|
|
12
|
+
|
|
13
|
+
from harness import match_brace, unit_record
|
|
14
|
+
|
|
15
|
+
EXTS = {".php"}
|
|
16
|
+
THROW = r"\bthrow\b"
|
|
17
|
+
FUNC_RE = re.compile(r"^function\s+&?([A-Za-z_]\w*)\s*\(([^)]*)\)\s*(?::\s*([?\w|\x5c]+))?\s*\{", re.M)
|
|
18
|
+
PARAM_RE = re.compile(r"^(?:([?\w|\x5c]+)\s+)?(&)?(\.\.\.)?\$(\w+)\s*(?:=\s*(.+))?$", re.S)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def runtime():
|
|
22
|
+
out = subprocess.run(["php", "-r", "echo PHP_VERSION;"], capture_output=True, text=True).stdout.strip()
|
|
23
|
+
return "php " + out
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def imports(text):
|
|
27
|
+
return sorted(set(re.findall(r"(?:require|include)(?:_once)?\s*\(?\s*['\"]([^'\"]+)['\"]", text)) |
|
|
28
|
+
set(re.findall(r"^use\s+([\w\x5c]+)", text, re.M)))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def split_params(raw):
|
|
32
|
+
parts, depth, cur = [], 0, ""
|
|
33
|
+
for c in raw:
|
|
34
|
+
depth += c in "([" and 1 or (c in ")]" and -1 or 0)
|
|
35
|
+
if c == "," and depth == 0:
|
|
36
|
+
parts.append(cur)
|
|
37
|
+
cur = ""
|
|
38
|
+
else:
|
|
39
|
+
cur += c
|
|
40
|
+
return parts + [cur]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def parse_params(raw):
|
|
44
|
+
params = []
|
|
45
|
+
for part in [p.strip() for p in split_params(raw) if p.strip()]:
|
|
46
|
+
m = PARAM_RE.match(part)
|
|
47
|
+
if m:
|
|
48
|
+
params.append({"name": m.group(4), "annotation": m.group(1), "default": (m.group(5) or "").strip() or None,
|
|
49
|
+
"kind": "varargs" if m.group(3) else "positional"})
|
|
50
|
+
return params
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def analyze_file(rel, text):
|
|
54
|
+
ns = (re.search(r"^namespace\s+([\w\x5c]+)\s*;", text, re.M) or [None, ""])[1]
|
|
55
|
+
module = ns.replace("\\", ".") or os.path.splitext(rel)[0].replace("/", ".")
|
|
56
|
+
units = []
|
|
57
|
+
for m in FUNC_RE.finditer(text):
|
|
58
|
+
end = match_brace(text, m.end() - 1)
|
|
59
|
+
units.append(unit_record(rel, module, m.group(1), m.start(), end, text, parse_params(m.group(2)), m.group(3), THROW))
|
|
60
|
+
return units
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
DRIVER = r'''<?php
|
|
64
|
+
error_reporting(E_ALL & ~E_DEPRECATED);
|
|
65
|
+
$ppTarget = realpath(__TARGET__);
|
|
66
|
+
$ppFn = __FN__;
|
|
67
|
+
|
|
68
|
+
function ppSpecial($s) {
|
|
69
|
+
switch ($s) {
|
|
70
|
+
case "nan": return NAN;
|
|
71
|
+
case "inf": return INF;
|
|
72
|
+
case "-inf": return -INF;
|
|
73
|
+
case "-0.0": return -0.0;
|
|
74
|
+
}
|
|
75
|
+
return (float)$s;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function ppDec($v) {
|
|
79
|
+
if (is_array($v)) {
|
|
80
|
+
if (count($v) === 1 && isset($v['$float']) && is_string($v['$float'])) {
|
|
81
|
+
return ppSpecial($v['$float']);
|
|
82
|
+
}
|
|
83
|
+
return array_map('ppDec', $v);
|
|
84
|
+
}
|
|
85
|
+
return $v;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function ppEnc($v) {
|
|
89
|
+
if (is_float($v)) {
|
|
90
|
+
if (is_nan($v)) return ['$float' => 'nan'];
|
|
91
|
+
if (is_infinite($v)) return ['$float' => $v > 0 ? 'inf' : '-inf'];
|
|
92
|
+
if ($v == 0 && fdiv(1, $v) < 0) return ['$float' => '-0.0'];
|
|
93
|
+
if ($v == floor($v) && abs($v) > 9007199254740992) return ['$float' => json_encode($v)];
|
|
94
|
+
return $v;
|
|
95
|
+
}
|
|
96
|
+
if (is_array($v)) {
|
|
97
|
+
if ($v === []) return [];
|
|
98
|
+
$out = array_map('ppEnc', $v);
|
|
99
|
+
return array_is_list($v) ? $out : (object)$out;
|
|
100
|
+
}
|
|
101
|
+
if (is_object($v)) return ppEnc(get_object_vars($v));
|
|
102
|
+
return $v;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function ppWrite($v) {
|
|
106
|
+
ini_set('serialize_precision', '-1');
|
|
107
|
+
file_put_contents(getenv('PP_RESULT'), json_encode($v, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION | JSON_INVALID_UTF8_SUBSTITUTE));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function ppLine($e, $target) {
|
|
111
|
+
if (realpath($e->getFile()) === $target) return $e->getLine();
|
|
112
|
+
foreach ($e->getTrace() as $f) {
|
|
113
|
+
if (isset($f['file']) && realpath($f['file']) === $target) return $f['line'];
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function ppCall($fn, $args, $target) {
|
|
119
|
+
try {
|
|
120
|
+
return ['kind' => 'return', 'value' => ppEnc($fn(...$args))];
|
|
121
|
+
} catch (Throwable $e) {
|
|
122
|
+
$o = ['kind' => 'raise', 'type' => get_class($e), 'message' => $e->getMessage()];
|
|
123
|
+
$line = ppLine($e, $target);
|
|
124
|
+
if ($line) $o['raise_line'] = $line;
|
|
125
|
+
return $o;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
require_once $ppTarget;
|
|
130
|
+
$req = json_decode(stream_get_contents(STDIN), true);
|
|
131
|
+
$args = ppDec($req['args'] ?? []);
|
|
132
|
+
if (($req['mode'] ?? 'call') === 'bench') {
|
|
133
|
+
$samples = [];
|
|
134
|
+
for ($i = 0; $i < ($req['iterations'] ?? 1); $i++) {
|
|
135
|
+
$t = hrtime(true);
|
|
136
|
+
ob_start();
|
|
137
|
+
ppCall($ppFn, $args, $ppTarget);
|
|
138
|
+
ob_end_clean();
|
|
139
|
+
$samples[] = hrtime(true) - $t;
|
|
140
|
+
}
|
|
141
|
+
ppWrite(['samples_ns' => $samples]);
|
|
142
|
+
} else {
|
|
143
|
+
ppWrite(['outcome' => ppCall($ppFn, $args, $ppTarget)]);
|
|
144
|
+
}
|
|
145
|
+
'''
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def prepare(src, rel, unit, work):
|
|
149
|
+
if unit["kind"] != "function":
|
|
150
|
+
raise SystemExit("only top-level PHP functions can be recorded")
|
|
151
|
+
ns = (re.search(r"^namespace\s+([\w\x5c]+)\s*;", open(os.path.join(src, rel), encoding="utf-8", errors="replace").read(), re.M) or [None, ""])[1]
|
|
152
|
+
fn = ("\\" + ns + "\\" if ns else "") + unit["name"]
|
|
153
|
+
code = DRIVER.replace("__TARGET__", json.dumps(os.path.join(src, rel))).replace("__FN__", json.dumps(fn))
|
|
154
|
+
path = os.path.join(work, "pp_driver.php")
|
|
155
|
+
open(path, "w", encoding="utf-8").write(code)
|
|
156
|
+
return ["php", "-d", "display_errors=stderr", "-d", "serialize_precision=-1", path]
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""Plain-Ruby (non-Rails source) support for the polyglot adapter.
|
|
2
|
+
|
|
3
|
+
Analysis: top-level `def name` and `def self.name` (module/class functions). Ruby has no types, so
|
|
4
|
+
annotations come from YARD `# @param x [Type]` comments, else from a literal default value.
|
|
5
|
+
Driver: pp_driver.rb loads the target file, decodes JSON args (with $float tags), finds the function on
|
|
6
|
+
the module(s) declared in the file (or Object for top-level defs), and maps exceptions to raise outcomes.
|
|
7
|
+
Run with `ruby --disable-gems`; only stdlib `json` is required.
|
|
8
|
+
"""
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
import subprocess
|
|
13
|
+
|
|
14
|
+
from harness import unit_record
|
|
15
|
+
|
|
16
|
+
EXTS = {".rb"}
|
|
17
|
+
THROW = r"\b(raise|fail)\b"
|
|
18
|
+
DEF_RE = re.compile(r"^([ \t]*)def\s+(?:self\.)?([A-Za-z_]\w*[?!]?)\s*(\(([^)]*)\))?([^\n]*)", re.M)
|
|
19
|
+
LITERAL_TYPES = [(r"^-?\d+$", "Integer"), (r"^-?\d+\.\d+$", "Float"), (r"^[\"']", "String"), (r"^(true|false)$", "bool"), (r"^\[", "Array")]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def runtime():
|
|
23
|
+
return "ruby " + subprocess.run(["ruby", "-e", "print RUBY_VERSION"], capture_output=True, text=True).stdout.strip()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def imports(text):
|
|
27
|
+
return sorted(set(re.findall(r"^\s*require(?:_relative)?\s+['\"]([^'\"]+)['\"]", text, re.M)))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def parse_params(raw, comment):
|
|
31
|
+
types = dict(re.findall(r"@param\s+(\w+)\s+\[([^\]]+)\]", comment or ""))
|
|
32
|
+
params = []
|
|
33
|
+
for part in [p.strip() for p in (raw or "").split(",") if p.strip()]:
|
|
34
|
+
kind, default = "positional", None
|
|
35
|
+
if part.startswith("&"):
|
|
36
|
+
continue
|
|
37
|
+
if part.startswith("**"):
|
|
38
|
+
kind, name = "kwargs", part[2:]
|
|
39
|
+
elif part.startswith("*"):
|
|
40
|
+
kind, name = "varargs", part[1:]
|
|
41
|
+
else:
|
|
42
|
+
m = re.match(r"(\w+)(:)?\s*(.*)$", part)
|
|
43
|
+
name, default = m.group(1), m.group(3).lstrip("=").strip() or None
|
|
44
|
+
if m.group(2):
|
|
45
|
+
kind = "keyword"
|
|
46
|
+
ann = types.get(name)
|
|
47
|
+
if not ann and default:
|
|
48
|
+
ann = next((t for rx, t in LITERAL_TYPES if re.match(rx, default)), None)
|
|
49
|
+
params.append({"name": name, "annotation": ann, "default": default, "kind": kind})
|
|
50
|
+
return params
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def analyze_file(rel, text):
|
|
54
|
+
module = os.path.splitext(rel)[0].replace("/", ".")
|
|
55
|
+
units = []
|
|
56
|
+
for m in DEF_RE.finditer(text):
|
|
57
|
+
indent, name, _paren, raw, rest = m.groups()
|
|
58
|
+
if re.match(r"^\s*=", rest) or re.match(r"^\s*\(?.*\)\s*=", rest): # endless def: one line
|
|
59
|
+
end = m.end() - 1
|
|
60
|
+
else:
|
|
61
|
+
em = re.compile(r"^" + indent + r"end\b", re.M).search(text, m.end())
|
|
62
|
+
end = em.end() - 1 if em else len(text) - 1
|
|
63
|
+
before = text[:m.start()].rstrip().split("\n")
|
|
64
|
+
comment = "\n".join(l for l in before[-6:] if l.strip().startswith("#"))
|
|
65
|
+
units.append(unit_record(rel, module, name, m.start(), end, text, parse_params(raw, comment), None, THROW))
|
|
66
|
+
return units
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
DRIVER = r'''require "json"
|
|
70
|
+
TARGET = File.realpath(__TARGET__)
|
|
71
|
+
FN = __FN__
|
|
72
|
+
MODS = __MODS__
|
|
73
|
+
|
|
74
|
+
def pp_special(s)
|
|
75
|
+
case s
|
|
76
|
+
when "nan" then Float::NAN
|
|
77
|
+
when "inf" then Float::INFINITY
|
|
78
|
+
when "-inf" then -Float::INFINITY
|
|
79
|
+
when "-0.0" then -0.0
|
|
80
|
+
else s.to_f
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def pp_dec(v)
|
|
85
|
+
case v
|
|
86
|
+
when Hash then v.size == 1 && v["$float"].is_a?(String) ? pp_special(v["$float"]) : v.transform_values { |x| pp_dec(x) }
|
|
87
|
+
when Array then v.map { |x| pp_dec(x) }
|
|
88
|
+
else v
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def pp_enc(v)
|
|
93
|
+
case v
|
|
94
|
+
when Float
|
|
95
|
+
return { "$float" => "nan" } if v.nan?
|
|
96
|
+
return { "$float" => v > 0 ? "inf" : "-inf" } if v.infinite?
|
|
97
|
+
return { "$float" => "-0.0" } if v.zero? && 1.0 / v < 0
|
|
98
|
+
return { "$float" => v.to_s } if v == v.floor && v.abs > 2**53
|
|
99
|
+
v
|
|
100
|
+
when Array then v.map { |x| pp_enc(x) }
|
|
101
|
+
when Hash then v.to_h { |k, x| [k.to_s, pp_enc(x)] }
|
|
102
|
+
when Symbol then v.to_s
|
|
103
|
+
when nil, true, false, Integer, String then v
|
|
104
|
+
else v.to_s
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def pp_line(e)
|
|
109
|
+
loc = (e.backtrace_locations || []).find { |l| File.realpath(l.absolute_path || l.path) == TARGET rescue false }
|
|
110
|
+
loc&.lineno
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def pp_call(recv, args)
|
|
114
|
+
{ "kind" => "return", "value" => pp_enc(recv.send(FN, *args)) }
|
|
115
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
116
|
+
o = { "kind" => "raise", "type" => e.class.name, "message" => e.message }
|
|
117
|
+
line = pp_line(e)
|
|
118
|
+
o["raise_line"] = line if line
|
|
119
|
+
o
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
load TARGET
|
|
123
|
+
recv = MODS.map { |m| Object.const_get(m) rescue nil }.compact.find { |m| m.respond_to?(FN) } || self
|
|
124
|
+
req = JSON.parse($stdin.read)
|
|
125
|
+
args = pp_dec(req["args"] || [])
|
|
126
|
+
if req["mode"] == "bench"
|
|
127
|
+
samples = Array.new(req["iterations"] || 1) do
|
|
128
|
+
t = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
|
|
129
|
+
pp_call(recv, args)
|
|
130
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - t
|
|
131
|
+
end
|
|
132
|
+
File.write(ENV["PP_RESULT"], JSON.generate("samples_ns" => samples))
|
|
133
|
+
else
|
|
134
|
+
File.write(ENV["PP_RESULT"], JSON.generate("outcome" => pp_call(recv, args)))
|
|
135
|
+
end
|
|
136
|
+
'''
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def prepare(src, rel, unit, work):
|
|
140
|
+
if unit["kind"] != "function":
|
|
141
|
+
raise SystemExit("only top-level Ruby functions can be recorded")
|
|
142
|
+
text = open(os.path.join(src, rel), encoding="utf-8", errors="replace").read()
|
|
143
|
+
mods = re.findall(r"^\s*(?:module|class)\s+([A-Z]\w*(?:::[A-Z]\w*)*)", text, re.M)
|
|
144
|
+
code = DRIVER.replace("__TARGET__", json.dumps(os.path.join(src, rel))).replace("__FN__", json.dumps(unit["name"])) \
|
|
145
|
+
.replace("__MODS__", json.dumps(mods))
|
|
146
|
+
path = os.path.join(work, "pp_driver.rb")
|
|
147
|
+
open(path, "w", encoding="utf-8").write(code)
|
|
148
|
+
return ["ruby", "--disable-gems", path]
|