Class: Shojiku::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/shojiku/library.rb

Overview

Finding and opening the engine's shared library.

Resolution order, and the deliberate asymmetry with the template root: SHOJIKU_LIBRARY beats explicit configuration, which beats the copy shipped inside the platform gem. That is the reverse of how the template root resolves, and on purpose — WHERE THE ENGINE LIVES is an operator/deployment decision that has to be able to win over application code, exactly as SHOJIKU_BIN does for the subprocess SDKs. WHICH TEMPLATES an application renders is the application's own decision, so there the explicit value wins.

Nothing here downloads anything. A library that is not present is a named error listing the install channels.

Constant Summary collapse

ABI_VERSION =

The ABI revision this gem is written against. It moves only when a symbol's meaning changes; new operations are appended without it, so a newer engine keeps working with this gem.

1
NAMES =

The names a platform gem's binary can have, in the order they are tried. Windows is the reason there are six rather than three: cargo emits shojiku_capi.dll with no lib prefix, while the Unix targets get one. Looking only for the prefixed form would make the gem unloadable on the platform the .NET market runs on.

%w[.so .dylib .dll].flat_map do |suffix|
  ["libshojiku_capi#{suffix}", "shojiku_capi#{suffix}"]
end.freeze
PACKAGED_DIR =

Where a platform gem puts the binary it ships.

File.expand_path("native", __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, env: Env.new(enabled: true), log: Log.new) ⇒ Library

Opens the library, or raises Shojiku::LibraryNotFound naming how to install it.

Raises:



40
41
42
43
44
45
46
47
48
# File 'lib/shojiku/library.rb', line 40

def initialize(path: nil, env: Env.new(enabled: true), log: Log.new)
  @log = log
  @path, @source = discover(path, env)
  raise LibraryNotFound, install_hint("no engine library was found") unless @path

  @handle = open_handle(@path)
  @log.event(:library_loaded, path: @path, source: @source)
  check_abi
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



37
38
39
# File 'lib/shojiku/library.rb', line 37

def path
  @path
end

Instance Method Details

#function(name, args, returns) ⇒ Object

A declared foreign function. Types are always explicit: Fiddle's defaults would return a C int and truncate every pointer this surface hands back.



53
54
55
56
57
# File 'lib/shojiku/library.rb', line 53

def function(name, args, returns)
  Fiddle::Function.new(@handle[name.to_s], args, returns)
rescue Fiddle::DLError => e
  raise LibraryNotFound, "#{@path} exports no `#{name}` (#{e.message})"
end