fluence_sdk_main/
result.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Contains ad-hoc implementations of returning complex data types from function calls
18//! by two global variables that contain pointer and size. Will be refactored after multi-value
19//! support in Wasmer.
20
21use std::sync::atomic::AtomicUsize;
22use std::cell::RefCell;
23use std::any::Any;
24
25static mut RESULT_PTR: AtomicUsize = AtomicUsize::new(0);
26static mut RESULT_SIZE: AtomicUsize = AtomicUsize::new(0);
27
28thread_local!(static OBJECTS_TO_RELEASE: RefCell<Vec<Box<dyn Any>>> = RefCell::new(Vec::new()));
29
30#[no_mangle]
31pub unsafe fn get_result_ptr() -> usize {
32    crate::debug_log!(format!(
33        "sdk.get_result_ptr, returns {}\n",
34        *RESULT_PTR.get_mut()
35    ));
36
37    *RESULT_PTR.get_mut()
38}
39
40#[no_mangle]
41pub unsafe fn get_result_size() -> usize {
42    crate::debug_log!(format!(
43        "sdk.get_result_size, returns {}\n",
44        *RESULT_SIZE.get_mut()
45    ));
46
47    *RESULT_SIZE.get_mut()
48}
49
50#[no_mangle]
51pub unsafe fn set_result_ptr(ptr: usize) {
52    crate::debug_log!(format!("sdk.set_result_ptr: {}\n", ptr));
53
54    *RESULT_PTR.get_mut() = ptr;
55}
56
57#[no_mangle]
58pub unsafe fn set_result_size(size: usize) {
59    crate::debug_log!(format!("sdk.set_result_size: {}\n", size));
60
61    *RESULT_SIZE.get_mut() = size;
62}
63
64#[no_mangle]
65pub unsafe fn release_objects() {
66    OBJECTS_TO_RELEASE.with(|objects| {
67        let mut objects = objects.borrow_mut();
68        while let Some(object) = objects.pop() {
69            drop(object);
70        }
71    })
72}
73
74pub fn add_object_to_release(object: Box<dyn Any>) {
75    OBJECTS_TO_RELEASE.with(|objects| {
76        let mut objects = objects.borrow_mut();
77        objects.push(object);
78    });
79}