Class: YAMLScript

Inherits:
Object
  • Object
show all
Defined in:
lib/yamlscript.rb,
lib/yamlscript/version.rb

Overview

Ruby binding for the libyamlscript shared library.

Defined Under Namespace

Modules: LibYAMLScript

Constant Summary collapse

Error =
Class.new(StandardError)
YAMLSCRIPT_VERSION =

This value is automatically updated by ‘make bump’. The version number is used to find the correct shared library file. We currently only support binding to an exact version of libyamlscript.

'0.1.49'
VERSION =
"0.1.49"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ YAMLScript

Returns a new instance of YAMLScript.



89
90
91
92
93
94
95
96
# File 'lib/yamlscript.rb', line 89

def initialize(**options)
  # config not used yet
  @options = options

  # Create a new GraalVM isolate for life of the YAMLScript instance
  @isolate = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



87
88
89
# File 'lib/yamlscript.rb', line 87

def error
  @error
end

#optionsObject (readonly)

Returns the value of attribute options.



87
88
89
# File 'lib/yamlscript.rb', line 87

def options
  @options
end

Class Method Details

.load(ys_code, **options) ⇒ Object

Interface with the libyamlscript shared library.

Examples:

require 'yamlscript'

YAMLScript.load(IO.read('file.ys'))


83
84
85
# File 'lib/yamlscript.rb', line 83

def self.load(ys_code, **options)
  new(**options).load(ys_code)
end

Instance Method Details

#load(ys_code) ⇒ Object

Compile and eval a YAMLScript string and return the result

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/yamlscript.rb', line 99

def load(ys_code)
  # Create a new GraalVM isolate thread for each call to load()
  thread = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
  raise Error, "Failed to create isolate" unless \
    LibYAMLScript.graal_create_isolate(nil, @isolate.ref, thread.ref).zero?

  # Call 'load_ys_to_json' function in libyamlscript shared library
  json_data = LibYAMLScript.load_ys_to_json(thread, ys_code)
  resp = JSON.parse(json_data.to_s)

  raise Error, "Failed to tear down isolate" unless \
    LibYAMLScript.graal_tear_down_isolate(thread).zero?
  raise Error, @error['cause'] if @error = resp['error']

  data = resp.fetch('data') do
    raise Error, "Unexpected response from 'libyamlscript'"
  end

  data
end