initialize

Function initialize 

Source
pub unsafe fn initialize(data: WolframLibraryData) -> Result<(), ()>
Expand description

Initialize static data for the current Wolfram library.

This function should be called during the execution of the WolframLibrary_initialize() hook provided by this library.

This function initializes the lazy Wolfram Runtime Library bindings in the rtl module.

§Safety

The following conditions must be met for a call to this function to be valid:

  • data must be a valid and fully initialized sys::WolframLibraryData instance created by the Wolfram Kernel and passed into the current LibraryLink function.
  • The call to initialize() must happen from the main Kernel thread. This is true for all LibraryLink functions called directly by the Kernel.

§Relation to #[init]

If the #[init] annotation is used to designate a library initialization function, initialize() will be called automatically.

§Example

Note: Prefer to use #[init] to designate an initialization function, instead of manually defining an unsafe initialization function as shown in this example.

When a dynamic library is loaded by the Wolfram Language (for example, via LibraryFunctionLoad), the system will call the function WolframLibrary_initialize() if one is provided by the library. This function is used to initialize callbacks from the library into functions provided by the Wolfram runtime.

use std::os::raw::c_int;
use wolfram_library_link::{sys, initialize};

#[no_mangle]
extern "C" fn WolframLibrary_initialize(data: sys::WolframLibraryData) -> c_int {
    match unsafe { initialize(data) } {
        Ok(()) => return 0,
        Err(()) => return 1,
    }
}
Examples found in repository?
examples/tests/test_data_store.rs (line 11)
10pub unsafe extern "C" fn WolframLibrary_initialize(lib: WolframLibraryData) -> c_int {
11    match wll::initialize(lib) {
12        Ok(()) => return 0,
13        Err(()) => return 1,
14    }
15}
More examples
Hide additional examples
examples/async/async_file_watcher_raw.rs (line 35)
23pub extern "C" fn start_file_watcher(
24    lib_data: sys::WolframLibraryData,
25    arg_count: mint,
26    args: *mut MArgument,
27    res: MArgument,
28) -> c_uint {
29    let args = unsafe { std::slice::from_raw_parts(args, arg_count as usize) };
30
31    if args.len() != 2 {
32        return LIBRARY_FUNCTION_ERROR;
33    }
34
35    if unsafe { wll::initialize(lib_data) }.is_err() {
36        return LIBRARY_FUNCTION_ERROR;
37    }
38
39    let task_arg = unsafe {
40        FileWatcherArgs {
41            pause_interval_ms: u64::try_from(*args[0].integer)
42                .expect("i64 interval overflows u64"),
43            path: {
44                let cstr = CStr::from_ptr(*args[1].utf8string);
45                match cstr.to_str() {
46                    Ok(s) => PathBuf::from(s),
47                    Err(_) => return LIBRARY_FUNCTION_ERROR,
48                }
49            },
50        }
51    };
52
53    // FIXME: This box is being leaked. Where is an appropriate place to drop it?
54    let task_arg = Box::into_raw(Box::new(task_arg)) as *mut c_void;
55
56    // Spawn a new thread, which will run in the background and check for file
57    // modifications.
58    unsafe {
59        let task_id: mint = wll::rtl::createAsynchronousTaskWithThread(
60            Some(file_watch_thread_function),
61            task_arg,
62        );
63        *res.integer = task_id;
64    }
65
66    LIBRARY_NO_ERROR
67}