Class: YAMLScript

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

Overview

Ruby binding for the libys shared library.

Defined Under Namespace

Modules: LibYS

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 libys.

'0.2.26'
VERSION =
"0.2.26"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ YAMLScript

Returns a new instance of YAMLScript.



102
103
104
105
106
107
108
109
# File 'lib/yamlscript.rb', line 102

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.



100
101
102
# File 'lib/yamlscript.rb', line 100

def error
  @error
end

#optionsObject (readonly)

Returns the value of attribute options.



100
101
102
# File 'lib/yamlscript.rb', line 100

def options
  @options
end

Class Method Details

.load(ys_code, **options) ⇒ Object

Interface with the libys shared library.

Examples:

require 'yamlscript'

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


96
97
98
# File 'lib/yamlscript.rb', line 96

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:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/yamlscript.rb', line 112

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 \
    LibYS.graal_create_isolate(nil, @isolate.ref, thread.ref).zero?

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

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

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

  data
end