polycomp 0.2.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.
package/src/bundle.rs CHANGED
@@ -25,13 +25,11 @@ const KDF_VM_SEALED: u8 = 3;
25
25
 
26
26
  const VERIFY_DOMAIN: &[u8] = b"POLYCOMP-IR-VERIFY-V3\0";
27
27
 
28
- // Embedded toolchain signing seed used when no explicit --sign-key is supplied.
29
28
  const DEFAULT_TOOLCHAIN_SIGN_SEED: [u8; 32] = [
30
29
  0x71, 0x4b, 0x92, 0xe8, 0x10, 0xc3, 0x55, 0xa9, 0x2d, 0x84, 0x0f, 0x6c, 0xbe, 0x37, 0x91, 0x44,
31
30
  0xd0, 0x62, 0x1a, 0xf3, 0x88, 0x4e, 0x29, 0x7c, 0x5b, 0xa1, 0xd6, 0x03, 0x99, 0xe5, 0x18, 0x6f,
32
31
  ];
33
32
 
34
- // Internal VM sealing domain material for keyless self-contained .polyx bundles.
35
33
  const VM_SEAL_MIX: [u8; 32] = [
36
34
  0xa4, 0x19, 0x7d, 0x52, 0xe0, 0x8c, 0x31, 0x6b, 0xf7, 0x42, 0x95, 0x0e, 0x83, 0xc6, 0x11, 0xda,
37
35
  0x58, 0x07, 0xbe, 0x64, 0x2a, 0xd9, 0x4f, 0x73, 0x91, 0xe2, 0x3c, 0x85, 0x16, 0xab, 0x60, 0xf4,
@@ -195,14 +193,6 @@ pub fn load_key_32_from_arg_or_file(val: &str) -> Result<[u8; 32], PolyError> {
195
193
  ))
196
194
  }
197
195
 
198
- /// Applies deep IR obfuscation:
199
- /// 1. Symbol/metadata stripping
200
- /// 2. IEEE-754 float bit-blinding (`Float` -> `I64` bits -> `BitsToF64`)
201
- /// 3. Mixed Boolean-Arithmetic (MBA) expression substitution on `I64`
202
- /// 4. Intra-block micro-splitting (chunk size 2)
203
- /// 5. Control-Flow Flattening (CFF) + opaque predicates + dead decoy blocks
204
- /// 6. Full integer constant blinding (`k -> (k ^ mask) ^ mask`) across program & CFF state IDs
205
- /// 7. Fisher-Yates basic block shuffling
206
196
  pub fn obfuscate_bundle(bundle: &mut IrBundle) -> Result<(), PolyError> {
207
197
  let mut seed = [0u8; 32];
208
198
  getrandom::getrandom(&mut seed)
@@ -230,7 +220,6 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
230
220
  return;
231
221
  }
232
222
 
233
- // 1. Blind Float constants into integer bit-patterns + BitsToF64 (later blinded + MBA'd!)
234
223
  for bb in &mut func.blocks {
235
224
  let mut out = Vec::with_capacity(bb.instructions.len());
236
225
  for instr in &bb.instructions {
@@ -247,13 +236,9 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
247
236
  bb.instructions = out;
248
237
  }
249
238
 
250
- // 2. Mixed Boolean-Arithmetic (MBA) on strictly I64 arithmetic/bitwise/neg ops
251
239
  apply_mba_pass(func, rng);
252
-
253
- // 3. Split large blocks into 2-instruction micro-blocks so sequential expressions break apart
254
240
  split_blocks_into_microchunks(func);
255
241
 
256
- // 4. Control-Flow Flattening (CFF) + Opaque Predicate + Dead Decoy Blocks
257
242
  const DISPATCH_PLACEHOLDER: usize = usize::MAX;
258
243
  let orig_blocks = std::mem::take(&mut func.blocks);
259
244
  let b_count = orig_blocks.len();
@@ -270,7 +255,6 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
270
255
  let r_state = Register(func.register_types.len());
271
256
  func.register_types.push(IrType::I64);
272
257
 
273
- // Opaque predicate in block 0: ((seed | 1) * (seed | 1)) & 1 == 1
274
258
  let mut entry_instrs = Vec::new();
275
259
  let seed_r = if let Some((pr, IrType::I64)) = func.params.first() {
276
260
  *pr
@@ -345,6 +329,10 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
345
329
  ins_out.push(IrInstruction::Return(opt));
346
330
  terminated = true;
347
331
  }
332
+ IrInstruction::Throw(r) => {
333
+ ins_out.push(IrInstruction::Throw(r));
334
+ terminated = true;
335
+ }
348
336
  IrInstruction::Jump(target) => {
349
337
  ins_out.push(IrInstruction::LoadConst(
350
338
  r_state,
@@ -401,7 +389,6 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
401
389
  new_blocks[3 + i].instructions = ins_out;
402
390
  }
403
391
 
404
- // 2 dead decoy blocks with realistic arithmetic
405
392
  let mut decoy_ids = Vec::with_capacity(2);
406
393
  for d_idx in 0..2 {
407
394
  let d_id = BasicBlockId(new_blocks.len());
@@ -425,7 +412,6 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
425
412
  });
426
413
  }
427
414
 
428
- // Shuffled CFF Dispatcher Chain
429
415
  let dispatch_start = BasicBlockId(new_blocks.len());
430
416
  let mut cases: Vec<(i64, BasicBlockId)> = (0..b_count)
431
417
  .map(|i| (states[i], real_ids[i]))
@@ -472,7 +458,6 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
472
458
  }
473
459
  }
474
460
 
475
- // 5. Constant Blinding: split every Int(k) into k ^ m1 ^ m2 with 2-stage XOR
476
461
  for bb in &mut new_blocks {
477
462
  let mut blinded = Vec::with_capacity(bb.instructions.len());
478
463
  for instr in &bb.instructions {
@@ -499,7 +484,6 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
499
484
  bb.instructions = blinded;
500
485
  }
501
486
 
502
- // 6. Shuffle blocks[1..] and remap all BasicBlockIds
503
487
  let n = new_blocks.len();
504
488
  let mut perm: Vec<usize> = (0..n).collect();
505
489
  for i in (1..n).rev() {
@@ -521,6 +505,9 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
521
505
  *a = BasicBlockId(inv[a.0]);
522
506
  *b = BasicBlockId(inv[b.0]);
523
507
  }
508
+ IrInstruction::TryBegin(catch_bb, _) => {
509
+ *catch_bb = BasicBlockId(inv[catch_bb.0]);
510
+ }
524
511
  _ => {}
525
512
  }
526
513
  }
@@ -529,7 +516,6 @@ fn obfuscate_function<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
529
516
  func.blocks = reordered;
530
517
  }
531
518
 
532
- /// Bit-exact two's-complement Mixed Boolean-Arithmetic (MBA) substitutions on I64 operations.
533
519
  fn apply_mba_pass<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
534
520
  let alloc_i64 = |reg_types: &mut Vec<IrType>| -> Register {
535
521
  let r = Register(reg_types.len());
@@ -646,7 +632,7 @@ fn apply_mba_pass<F: FnMut() -> u64>(func: &mut IrFunction, rng: &mut F) {
646
632
  bb.instructions = out;
647
633
  }
648
634
  }
649
- /// Splits basic blocks with >2 instructions into 2-instruction chunks linked by `Jump`.
635
+
650
636
  fn split_blocks_into_microchunks(func: &mut IrFunction) {
651
637
  let orig = std::mem::take(&mut func.blocks);
652
638
  let mut chunk_groups: Vec<Vec<Vec<IrInstruction>>> = Vec::with_capacity(orig.len());
@@ -664,7 +650,6 @@ fn split_blocks_into_microchunks(func: &mut IrFunction) {
664
650
  .chunks(2)
665
651
  .map(|c| c.to_vec())
666
652
  .collect();
667
- // Avoid isolating a single terminator in a trailing 1-instr chunk if avoidable
668
653
  if chunks.len() >= 2 && chunks.last().is_some_and(|c| c.len() == 1) {
669
654
  let last = chunks.pop().unwrap();
670
655
  chunks.last_mut().unwrap().extend(last);
@@ -686,6 +671,9 @@ fn split_blocks_into_microchunks(func: &mut IrFunction) {
686
671
  *a = old_to_first_new[a.0];
687
672
  *b = old_to_first_new[b.0];
688
673
  }
674
+ IrInstruction::TryBegin(catch_bb, _) => {
675
+ *catch_bb = old_to_first_new[catch_bb.0];
676
+ }
689
677
  _ => {}
690
678
  }
691
679
  }
@@ -788,7 +776,6 @@ pub fn encode_container(bundle: &IrBundle, opts: &SaveOptions) -> Result<Vec<u8>
788
776
  return Ok(text.into_bytes());
789
777
  }
790
778
 
791
- // Pure UTF-8 Armored Container (POLYX003) — keyless (`vm-sealed`) OR key-locked
792
779
  let mut nonce = [0u8; 24];
793
780
  getrandom::getrandom(&mut nonce)
794
781
  .map_err(|e| PolyError::msg("E0702", format!("CSPRNG error: {e}")))?;
@@ -905,7 +892,7 @@ pub fn verify_container_bytes(
905
892
  let Some(sig_info) = &parsed.sig_info else {
906
893
  return Err(PolyError::msg(
907
894
  "E0712",
908
- "Part 1 Verifier failed: unsigned .polyir container (sign with `polycomp sign` or pass `--allow-unsigned` to `run`)",
895
+ "Part 1 Verifier failed: unsigned .polyir container",
909
896
  ));
910
897
  };
911
898
  let meta = format_plain_meta(
@@ -944,7 +931,7 @@ pub fn verify_container_bytes(
944
931
  } else {
945
932
  Err(PolyError::msg(
946
933
  "E0707",
947
- "unrecognized PolyComp UTF-8 container header (expected POLYIR03 or POLYX003)",
934
+ "unrecognized PolyComp UTF-8 container header",
948
935
  ))
949
936
  }
950
937
  }
@@ -1127,7 +1114,7 @@ fn verify_signature_and_pin(
1127
1114
  verifying_key.verify_strict(sig_msg, &signature).map_err(|_| {
1128
1115
  PolyError::msg(
1129
1116
  "E0712",
1130
- "Part 1 Verifier failed: Ed25519 signature invalid (tampered or forged artifact)",
1117
+ "Part 1 Verifier failed: Ed25519 signature invalid",
1131
1118
  )
1132
1119
  })?;
1133
1120
 
@@ -1147,7 +1134,6 @@ fn compute_origin_fingerprint(pubkey: &[u8; 32], flags: u8, salt: &[u8; 16]) ->
1147
1134
  h.finalize().into()
1148
1135
  }
1149
1136
 
1150
- /// Multi-round ARX + BLAKE3 whitebox key schedule for keyless VM-sealed payloads.
1151
1137
  fn derive_vm_sealed_key(salt: &[u8; 16], nonce: &[u8; 24], pubkey: &[u8; 32]) -> [u8; 32] {
1152
1138
  let mut state: [u64; 4] = [0; 4];
1153
1139
  for (i, slot) in state.iter_mut().enumerate() {
@@ -1230,10 +1216,6 @@ fn derive_aead_key(
1230
1216
  }
1231
1217
  }
1232
1218
 
1233
- // ============================================================================
1234
- // Rolling Stream-Whitened + Opcode-Permuted VM Bytecode + Chaff + Decoy Strings
1235
- // ============================================================================
1236
-
1237
1219
  struct OpcodeSbox {
1238
1220
  enc: [u8; 256],
1239
1221
  dec: [u8; 256],
@@ -1333,7 +1315,6 @@ impl PermBinWriter {
1333
1315
  }
1334
1316
  fn str(&mut self, s: &str) -> Result<(), PolyError> {
1335
1317
  let bytes = s.as_bytes();
1336
- // Interleave random-length decoy padding inside each string record
1337
1318
  let mut pad_byte = [0u8; 1];
1338
1319
  getrandom::getrandom(&mut pad_byte)
1339
1320
  .map_err(|e| PolyError::msg("E0702", format!("CSPRNG error: {e}")))?;
@@ -1426,7 +1407,6 @@ fn serialize_permuted_bytecode(
1426
1407
  ) -> Result<Vec<u8>, PolyError> {
1427
1408
  let mut w = PermBinWriter::new(*salt, *nonce);
1428
1409
 
1429
- // Random-length leading chaff padding
1430
1410
  let mut chaff_rng = [0u8; 2];
1431
1411
  getrandom::getrandom(&mut chaff_rng)
1432
1412
  .map_err(|e| PolyError::msg("E0702", format!("CSPRNG error: {e}")))?;
@@ -1633,6 +1613,11 @@ fn encode_perm_instr(w: &mut PermBinWriter, instr: &IrInstruction) -> Result<(),
1633
1613
  w.op(4);
1634
1614
  w.str(s)?;
1635
1615
  }
1616
+ Constant::Regex { pattern, flags } => {
1617
+ w.op(5);
1618
+ w.str(pattern)?;
1619
+ w.str(flags)?;
1620
+ }
1636
1621
  }
1637
1622
  }
1638
1623
  IrInstruction::Move(d, s) => {
@@ -1764,6 +1749,25 @@ fn encode_perm_instr(w: &mut PermBinWriter, instr: &IrInstruction) -> Result<(),
1764
1749
  w.u32(b.0 as u32);
1765
1750
  w.u32(o.0 as u32);
1766
1751
  }
1752
+ IrInstruction::CallMethod(d, o, m, args) => {
1753
+ w.op(24);
1754
+ w.u32(d.0 as u32);
1755
+ w.u32(o.0 as u32);
1756
+ w.str(m)?;
1757
+ encode_perm_reg_list(w, args);
1758
+ }
1759
+ IrInstruction::TryBegin(catch_bb, r) => {
1760
+ w.op(25);
1761
+ w.u32(catch_bb.0 as u32);
1762
+ w.u32(r.0 as u32);
1763
+ }
1764
+ IrInstruction::TryEnd => {
1765
+ w.op(26);
1766
+ }
1767
+ IrInstruction::Throw(r) => {
1768
+ w.op(27);
1769
+ w.u32(r.0 as u32);
1770
+ }
1767
1771
  }
1768
1772
  Ok(())
1769
1773
  }
@@ -1779,6 +1783,10 @@ fn decode_perm_instr(r: &mut PermBinReader<'_>) -> Result<IrInstruction, PolyErr
1779
1783
  2 => Constant::Int(r.i64()?),
1780
1784
  3 => Constant::Float(r.f64()?),
1781
1785
  4 => Constant::String(r.str()?),
1786
+ 5 => Constant::Regex {
1787
+ pattern: r.str()?,
1788
+ flags: r.str()?,
1789
+ },
1782
1790
  _ => return Err(PolyError::msg("E0716", "invalid Constant tag")),
1783
1791
  };
1784
1792
  Ok(IrInstruction::LoadConst(d, c))
@@ -1894,6 +1902,18 @@ fn decode_perm_instr(r: &mut PermBinReader<'_>) -> Result<IrInstruction, PolyErr
1894
1902
  Register(r.u32()? as usize),
1895
1903
  Register(r.u32()? as usize),
1896
1904
  )),
1905
+ 24 => Ok(IrInstruction::CallMethod(
1906
+ Register(r.u32()? as usize),
1907
+ Register(r.u32()? as usize),
1908
+ r.str()?,
1909
+ decode_perm_reg_list(r)?,
1910
+ )),
1911
+ 25 => Ok(IrInstruction::TryBegin(
1912
+ BasicBlockId(r.u32()? as usize),
1913
+ Register(r.u32()? as usize),
1914
+ )),
1915
+ 26 => Ok(IrInstruction::TryEnd),
1916
+ 27 => Ok(IrInstruction::Throw(Register(r.u32()? as usize))),
1897
1917
  _ => Err(PolyError::msg("E0716", "invalid VM instruction tag")),
1898
1918
  }
1899
1919
  }
@@ -1906,6 +1926,7 @@ fn decode_binop(v: u8) -> Result<IrBinOp, PolyError> {
1906
1926
  x if x == IrBinOp::Div as u8 => Ok(IrBinOp::Div),
1907
1927
  x if x == IrBinOp::FloorDiv as u8 => Ok(IrBinOp::FloorDiv),
1908
1928
  x if x == IrBinOp::Mod as u8 => Ok(IrBinOp::Mod),
1929
+ x if x == IrBinOp::Pow as u8 => Ok(IrBinOp::Pow),
1909
1930
  x if x == IrBinOp::Eq as u8 => Ok(IrBinOp::Eq),
1910
1931
  x if x == IrBinOp::Ne as u8 => Ok(IrBinOp::Ne),
1911
1932
  x if x == IrBinOp::Lt as u8 => Ok(IrBinOp::Lt),
@@ -1938,6 +1959,7 @@ fn decode_builtin(v: u32) -> Result<BuiltinId, PolyError> {
1938
1959
  x if x == BuiltinId::Print as u32 => Ok(BuiltinId::Print),
1939
1960
  x if x == BuiltinId::Println as u32 => Ok(BuiltinId::Println),
1940
1961
  x if x == BuiltinId::Printf as u32 => Ok(BuiltinId::Printf),
1962
+ x if x == BuiltinId::Sprintf as u32 => Ok(BuiltinId::Sprintf),
1941
1963
  x if x == BuiltinId::Len as u32 => Ok(BuiltinId::Len),
1942
1964
  x if x == BuiltinId::Str as u32 => Ok(BuiltinId::Str),
1943
1965
  x if x == BuiltinId::Int as u32 => Ok(BuiltinId::Int),
@@ -1948,6 +1970,12 @@ fn decode_builtin(v: u32) -> Result<BuiltinId, PolyError> {
1948
1970
  x if x == BuiltinId::Max as u32 => Ok(BuiltinId::Max),
1949
1971
  x if x == BuiltinId::Sqrt as u32 => Ok(BuiltinId::Sqrt),
1950
1972
  x if x == BuiltinId::Pow as u32 => Ok(BuiltinId::Pow),
1973
+ x if x == BuiltinId::Floor as u32 => Ok(BuiltinId::Floor),
1974
+ x if x == BuiltinId::Ceil as u32 => Ok(BuiltinId::Ceil),
1975
+ x if x == BuiltinId::Round as u32 => Ok(BuiltinId::Round),
1976
+ x if x == BuiltinId::Sin as u32 => Ok(BuiltinId::Sin),
1977
+ x if x == BuiltinId::Cos as u32 => Ok(BuiltinId::Cos),
1978
+ x if x == BuiltinId::Log as u32 => Ok(BuiltinId::Log),
1951
1979
  x if x == BuiltinId::Push as u32 => Ok(BuiltinId::Push),
1952
1980
  x if x == BuiltinId::Pop as u32 => Ok(BuiltinId::Pop),
1953
1981
  x if x == BuiltinId::TypeOf as u32 => Ok(BuiltinId::TypeOf),
@@ -1957,14 +1985,23 @@ fn decode_builtin(v: u32) -> Result<BuiltinId, PolyError> {
1957
1985
  x if x == BuiltinId::BufLen as u32 => Ok(BuiltinId::BufLen),
1958
1986
  x if x == BuiltinId::ClockMs as u32 => Ok(BuiltinId::ClockMs),
1959
1987
  x if x == BuiltinId::Assert as u32 => Ok(BuiltinId::Assert),
1988
+ x if x == BuiltinId::Range as u32 => Ok(BuiltinId::Range),
1989
+ x if x == BuiltinId::RegexNew as u32 => Ok(BuiltinId::RegexNew),
1990
+ x if x == BuiltinId::RegexTest as u32 => Ok(BuiltinId::RegexTest),
1991
+ x if x == BuiltinId::RegexMatch as u32 => Ok(BuiltinId::RegexMatch),
1992
+ x if x == BuiltinId::RegexFindAll as u32 => Ok(BuiltinId::RegexFindAll),
1993
+ x if x == BuiltinId::RegexReplace as u32 => Ok(BuiltinId::RegexReplace),
1994
+ x if x == BuiltinId::RegexSplit as u32 => Ok(BuiltinId::RegexSplit),
1995
+ x if x == BuiltinId::StrContains as u32 => Ok(BuiltinId::StrContains),
1996
+ x if x == BuiltinId::StrSplit as u32 => Ok(BuiltinId::StrSplit),
1997
+ x if x == BuiltinId::StrTrim as u32 => Ok(BuiltinId::StrTrim),
1998
+ x if x == BuiltinId::StrUpper as u32 => Ok(BuiltinId::StrUpper),
1999
+ x if x == BuiltinId::StrLower as u32 => Ok(BuiltinId::StrLower),
2000
+ x if x == BuiltinId::StrReplace as u32 => Ok(BuiltinId::StrReplace),
1960
2001
  _ => Err(PolyError::msg("E0716", "invalid BuiltinId tag")),
1961
2002
  }
1962
2003
  }
1963
2004
 
1964
- // ============================================================================
1965
- // Armored POLYX003 Parser
1966
- // ============================================================================
1967
-
1968
2005
  struct ParsedArmored {
1969
2006
  canonical_header: String,
1970
2007
  kdf_mode: u8,
@@ -2086,10 +2123,6 @@ fn parse_armored_polyx(text: &str) -> Result<ParsedArmored, PolyError> {
2086
2123
  })
2087
2124
  }
2088
2125
 
2089
- // ============================================================================
2090
- // Pure UTF-8 Human-Readable IR Formatter & Parser (POLYIR03)
2091
- // ============================================================================
2092
-
2093
2126
  struct PlainSigInfo {
2094
2127
  salt: [u8; 16],
2095
2128
  pubkey_bytes: [u8; 32],
@@ -2491,6 +2524,15 @@ fn format_instr(out: &mut String, instr: &IrInstruction) {
2491
2524
  Constant::String(s) => {
2492
2525
  let _ = write!(out, "load_const r{} str {}", d.0, escape_str(s));
2493
2526
  }
2527
+ Constant::Regex { pattern, flags } => {
2528
+ let _ = write!(
2529
+ out,
2530
+ "load_const r{} regex {} {}",
2531
+ d.0,
2532
+ escape_str(pattern),
2533
+ escape_str(flags)
2534
+ );
2535
+ }
2494
2536
  },
2495
2537
  IrInstruction::Move(d, s) => {
2496
2538
  let _ = write!(out, "move r{} r{}", d.0, s.0);
@@ -2571,6 +2613,25 @@ fn format_instr(out: &mut String, instr: &IrInstruction) {
2571
2613
  IrInstruction::LoadByte(d, b, o) => {
2572
2614
  let _ = write!(out, "load_byte r{} r{} r{}", d.0, b.0, o.0);
2573
2615
  }
2616
+ IrInstruction::CallMethod(d, o, m, args) => {
2617
+ let _ = write!(
2618
+ out,
2619
+ "call_method r{} r{} {} {}",
2620
+ d.0,
2621
+ o.0,
2622
+ escape_str(m),
2623
+ format_reg_list(args)
2624
+ );
2625
+ }
2626
+ IrInstruction::TryBegin(catch_bb, r) => {
2627
+ let _ = write!(out, "try_begin bb{} r{}", catch_bb.0, r.0);
2628
+ }
2629
+ IrInstruction::TryEnd => {
2630
+ out.push_str("try_end");
2631
+ }
2632
+ IrInstruction::Throw(r) => {
2633
+ let _ = write!(out, "throw r{}", r.0);
2634
+ }
2574
2635
  }
2575
2636
  }
2576
2637
 
@@ -2611,6 +2672,14 @@ fn parse_instr_line(line: &str) -> Result<IrInstruction, PolyError> {
2611
2672
  Constant::Float(f64::from_bits(bits))
2612
2673
  }
2613
2674
  "str" => Constant::String(parse_escaped_str(val_str)?.0),
2675
+ "regex" => {
2676
+ let (pat, r2) = parse_escaped_str(val_str)?;
2677
+ let (flags, _) = parse_escaped_str(r2.trim())?;
2678
+ Constant::Regex {
2679
+ pattern: pat,
2680
+ flags,
2681
+ }
2682
+ }
2614
2683
  _ => return Err(PolyError::msg("E0716", format!("unknown constant kind '{kind}'"))),
2615
2684
  };
2616
2685
  Ok(IrInstruction::LoadConst(dst, c))
@@ -2779,6 +2848,26 @@ fn parse_instr_line(line: &str) -> Result<IrInstruction, PolyError> {
2779
2848
  parse_reg(o)?,
2780
2849
  ))
2781
2850
  }
2851
+ "call_method" => {
2852
+ let (d, r1) = split_ws_tok(rest)?;
2853
+ let (o, r2) = split_ws_tok(r1)?;
2854
+ let (m, args_tok) = parse_escaped_str(r2)?;
2855
+ Ok(IrInstruction::CallMethod(
2856
+ parse_reg(d)?,
2857
+ parse_reg(o)?,
2858
+ m,
2859
+ parse_reg_list(args_tok)?,
2860
+ ))
2861
+ }
2862
+ "try_begin" => {
2863
+ let (bb_tok, r_tok) = split_ws_tok(rest)?;
2864
+ Ok(IrInstruction::TryBegin(
2865
+ parse_bb(bb_tok)?,
2866
+ parse_reg(r_tok)?,
2867
+ ))
2868
+ }
2869
+ "try_end" => Ok(IrInstruction::TryEnd),
2870
+ "throw" => Ok(IrInstruction::Throw(parse_reg(rest)?)),
2782
2871
  other => Err(PolyError::msg(
2783
2872
  "E0716",
2784
2873
  format!("unknown IR opcode '{other}'"),
@@ -2944,6 +3033,7 @@ fn binop_name(op: IrBinOp) -> &'static str {
2944
3033
  IrBinOp::Div => "Div",
2945
3034
  IrBinOp::FloorDiv => "FloorDiv",
2946
3035
  IrBinOp::Mod => "Mod",
3036
+ IrBinOp::Pow => "Pow",
2947
3037
  IrBinOp::Eq => "Eq",
2948
3038
  IrBinOp::Ne => "Ne",
2949
3039
  IrBinOp::Lt => "Lt",
@@ -2968,6 +3058,7 @@ fn parse_binop_name(s: &str) -> Result<IrBinOp, PolyError> {
2968
3058
  "Div" => Ok(IrBinOp::Div),
2969
3059
  "FloorDiv" => Ok(IrBinOp::FloorDiv),
2970
3060
  "Mod" => Ok(IrBinOp::Mod),
3061
+ "Pow" => Ok(IrBinOp::Pow),
2971
3062
  "Eq" => Ok(IrBinOp::Eq),
2972
3063
  "Ne" => Ok(IrBinOp::Ne),
2973
3064
  "Lt" => Ok(IrBinOp::Lt),
@@ -3009,6 +3100,7 @@ fn builtin_name(b: BuiltinId) -> &'static str {
3009
3100
  BuiltinId::Print => "Print",
3010
3101
  BuiltinId::Println => "Println",
3011
3102
  BuiltinId::Printf => "Printf",
3103
+ BuiltinId::Sprintf => "Sprintf",
3012
3104
  BuiltinId::Len => "Len",
3013
3105
  BuiltinId::Str => "Str",
3014
3106
  BuiltinId::Int => "Int",
@@ -3019,6 +3111,12 @@ fn builtin_name(b: BuiltinId) -> &'static str {
3019
3111
  BuiltinId::Max => "Max",
3020
3112
  BuiltinId::Sqrt => "Sqrt",
3021
3113
  BuiltinId::Pow => "Pow",
3114
+ BuiltinId::Floor => "Floor",
3115
+ BuiltinId::Ceil => "Ceil",
3116
+ BuiltinId::Round => "Round",
3117
+ BuiltinId::Sin => "Sin",
3118
+ BuiltinId::Cos => "Cos",
3119
+ BuiltinId::Log => "Log",
3022
3120
  BuiltinId::Push => "Push",
3023
3121
  BuiltinId::Pop => "Pop",
3024
3122
  BuiltinId::TypeOf => "TypeOf",
@@ -3028,6 +3126,19 @@ fn builtin_name(b: BuiltinId) -> &'static str {
3028
3126
  BuiltinId::BufLen => "BufLen",
3029
3127
  BuiltinId::ClockMs => "ClockMs",
3030
3128
  BuiltinId::Assert => "Assert",
3129
+ BuiltinId::Range => "Range",
3130
+ BuiltinId::RegexNew => "RegexNew",
3131
+ BuiltinId::RegexTest => "RegexTest",
3132
+ BuiltinId::RegexMatch => "RegexMatch",
3133
+ BuiltinId::RegexFindAll => "RegexFindAll",
3134
+ BuiltinId::RegexReplace => "RegexReplace",
3135
+ BuiltinId::RegexSplit => "RegexSplit",
3136
+ BuiltinId::StrContains => "StrContains",
3137
+ BuiltinId::StrSplit => "StrSplit",
3138
+ BuiltinId::StrTrim => "StrTrim",
3139
+ BuiltinId::StrUpper => "StrUpper",
3140
+ BuiltinId::StrLower => "StrLower",
3141
+ BuiltinId::StrReplace => "StrReplace",
3031
3142
  }
3032
3143
  }
3033
3144
 
@@ -3036,6 +3147,7 @@ fn parse_builtin_name(s: &str) -> Result<BuiltinId, PolyError> {
3036
3147
  "Print" => Ok(BuiltinId::Print),
3037
3148
  "Println" => Ok(BuiltinId::Println),
3038
3149
  "Printf" => Ok(BuiltinId::Printf),
3150
+ "Sprintf" => Ok(BuiltinId::Sprintf),
3039
3151
  "Len" => Ok(BuiltinId::Len),
3040
3152
  "Str" => Ok(BuiltinId::Str),
3041
3153
  "Int" => Ok(BuiltinId::Int),
@@ -3046,6 +3158,12 @@ fn parse_builtin_name(s: &str) -> Result<BuiltinId, PolyError> {
3046
3158
  "Max" => Ok(BuiltinId::Max),
3047
3159
  "Sqrt" => Ok(BuiltinId::Sqrt),
3048
3160
  "Pow" => Ok(BuiltinId::Pow),
3161
+ "Floor" => Ok(BuiltinId::Floor),
3162
+ "Ceil" => Ok(BuiltinId::Ceil),
3163
+ "Round" => Ok(BuiltinId::Round),
3164
+ "Sin" => Ok(BuiltinId::Sin),
3165
+ "Cos" => Ok(BuiltinId::Cos),
3166
+ "Log" => Ok(BuiltinId::Log),
3049
3167
  "Push" => Ok(BuiltinId::Push),
3050
3168
  "Pop" => Ok(BuiltinId::Pop),
3051
3169
  "TypeOf" => Ok(BuiltinId::TypeOf),
@@ -3055,14 +3173,23 @@ fn parse_builtin_name(s: &str) -> Result<BuiltinId, PolyError> {
3055
3173
  "BufLen" => Ok(BuiltinId::BufLen),
3056
3174
  "ClockMs" => Ok(BuiltinId::ClockMs),
3057
3175
  "Assert" => Ok(BuiltinId::Assert),
3176
+ "Range" => Ok(BuiltinId::Range),
3177
+ "RegexNew" => Ok(BuiltinId::RegexNew),
3178
+ "RegexTest" => Ok(BuiltinId::RegexTest),
3179
+ "RegexMatch" => Ok(BuiltinId::RegexMatch),
3180
+ "RegexFindAll" => Ok(BuiltinId::RegexFindAll),
3181
+ "RegexReplace" => Ok(BuiltinId::RegexReplace),
3182
+ "RegexSplit" => Ok(BuiltinId::RegexSplit),
3183
+ "StrContains" => Ok(BuiltinId::StrContains),
3184
+ "StrSplit" => Ok(BuiltinId::StrSplit),
3185
+ "StrTrim" => Ok(BuiltinId::StrTrim),
3186
+ "StrUpper" => Ok(BuiltinId::StrUpper),
3187
+ "StrLower" => Ok(BuiltinId::StrLower),
3188
+ "StrReplace" => Ok(BuiltinId::StrReplace),
3058
3189
  other => Err(PolyError::msg("E0716", format!("unknown BuiltinId '{other}'"))),
3059
3190
  }
3060
3191
  }
3061
3192
 
3062
- // ============================================================================
3063
- // Pure-Rust RFC 4648 Base64 + Hex Utilities
3064
- // ============================================================================
3065
-
3066
3193
  const B64_TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3067
3194
 
3068
3195
  fn base64_encode_wrapped(data: &[u8], line_width: usize) -> String {