Class: Schematic::RulesEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/schematic/rules_engine.rb

Constant Summary collapse

WASM_PATH =
File.join(__dir__, "wasm", "rulesengine.wasm")

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ RulesEngine

Returns a new instance of RulesEngine.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/schematic/rules_engine.rb', line 9

def initialize(logger: nil)
  @logger = logger || ConsoleLogger.new
  @initialized = false
  @mutex = Mutex.new
  @store = nil
  @memory = nil
  @alloc_fn = nil
  @dealloc_fn = nil
  @check_flag_fn = nil
  @get_result_json_fn = nil
  @get_result_json_length_fn = nil
  @get_version_key_fn = nil
end

Instance Method Details

#check_flag(flag, company = nil, user = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/schematic/rules_engine.rb', line 69

def check_flag(flag, company = nil, user = nil)
  raise "WASM rules engine not initialized" unless @initialized

  # Build combined JSON envelope (same format as Python/C#)
  envelope = { flag: strip_nulls(flag) }
  envelope[:company] = strip_nulls(company) if company
  envelope[:user] = strip_nulls(user) if user

  json_bytes = JSON.generate(envelope).encode("UTF-8")

  @mutex.synchronize do
    ptr = @alloc_fn.call(json_bytes.bytesize)
    begin
      @memory.write(ptr, json_bytes)
      result_len = @check_flag_fn.call(ptr, json_bytes.bytesize)

      raise "WASM checkFlagCombined returned error" if result_len.negative?

      result_ptr = @get_result_json_fn.call
      actual_len = @get_result_json_length_fn.call
      result_json = @memory.read(result_ptr, actual_len).force_encoding("UTF-8")

      normalize_keys(JSON.parse(result_json, symbolize_names: true))
    ensure
      @dealloc_fn.call(ptr, json_bytes.bytesize)
    end
  end
rescue StandardError => e
  @logger.error("WASM check_flag failed: #{e.message}")
  raise
end

#initialize!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/schematic/rules_engine.rb', line 23

def initialize!
  @mutex.synchronize do
    return if @initialized

    unless File.exist?(WASM_PATH)
      @logger.warn("WASM binary not found at #{WASM_PATH}. Run scripts/download-wasm.sh to fetch it.")
      return
    end

    require "wasmtime"

    engine = Wasmtime::Engine.new
    mod = Wasmtime::Module.from_file(engine, WASM_PATH)

    linker = Wasmtime::Linker.new(engine)

    wasi_config = Wasmtime::WasiConfig.new
    wasi_config.set_stdin_string("")
    @store = Wasmtime::Store.new(engine, wasi_config: wasi_config)

    @instance = linker.instantiate(@store, mod)

    # Get raw WASM exports (C ABI — no wasm-bindgen)
    @memory = @instance.export("memory").to_memory
    @alloc_fn = export_func("alloc")
    @dealloc_fn = export_func("dealloc")
    @check_flag_fn = export_func("checkFlagCombined")
    @get_result_json_fn = export_func("getResultJson")
    @get_result_json_length_fn = export_func("getResultJsonLength")
    @get_version_key_fn = export_func("get_version_key_wasm")

    @initialized = true
    @logger.debug("WASM rules engine initialized")
  rescue LoadError => e
    @logger.warn("wasmtime gem not available, WASM rules engine disabled: #{e.message}")
    @initialized = false
  rescue StandardError => e
    @logger.error("Failed to initialize WASM rules engine: #{e.message}")
    @initialized = false
  end
end

#initialized?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/schematic/rules_engine.rb', line 65

def initialized?
  @initialized
end

#version_keyObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/schematic/rules_engine.rb', line 101

def version_key
  raise "WASM rules engine not initialized" unless @initialized

  @mutex.synchronize do
    ptr = @get_version_key_fn.call
    # Read null-terminated string from WASM memory
    raw = @memory.read(ptr, 256)
    null_idx = raw.index("\0") || raw.bytesize
    raw[0, null_idx].force_encoding("UTF-8")
  end
end