-
Notifications
You must be signed in to change notification settings - Fork 5
/
swift.cc
86 lines (69 loc) · 2.33 KB
/
swift.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>
#include <nan.h>
#include <dlfcn.h>
using std::cerr;
using std::endl;
using std::string;
#ifdef __APPLE__
#define IS_SUPPORTED_PLATFORM true
const string SHARED_LIBRARY_EXT = "dylib";
#endif
#ifdef __linux__
#define IS_SUPPORTED_PLATFORM true
const string SHARED_LIBRARY_EXT = "so";
#endif
#if !defined IS_SUPPORTED_PLATFORM
cerr << "Unsupported Platform";
exit(EXIT_FAILURE);
#endif
const string SWIFT_SHARED_LIBRARY_PATH = "./NativeExtensionInSwift/.build/debug/libNativeExtensionInSwift."+SHARED_LIBRARY_EXT;
const auto SWIFT_FIBONACCI_FUNC_SYMBOL = "swift_fibonacci";
const auto SWIFT_PRINT_HELLO_FUNC_SYMBOL = "swift_print_hello";
typedef int (*FibonacciFunc)(int);
typedef void (*PrintHelloFunc)();
void *DLSymOrDie(void *lib, const string& func_name) {
const auto func = dlsym(lib, func_name.c_str());
const auto dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Could not load symbol create: " << dlsym_error << endl;
dlclose(lib);
exit(EXIT_FAILURE);
}
return func;
}
void *DLOpenOrDie(const string& path) {
const auto lib = dlopen(path.c_str(), RTLD_LAZY);
if (!lib) {
cerr << "Could not load library: " << dlerror() << endl;
exit(EXIT_FAILURE);
}
return lib;
}
void Fibonacci(const Nan::FunctionCallbackInfo<v8::Value>& info) {
if (info.Length() != 1) {
Nan::ThrowTypeError("Wrong number of arguments");
return;
}
if (!info[0]->IsNumber()) {
Nan::ThrowTypeError("Argument should be a number");
return;
}
const auto swiftLib = DLOpenOrDie(SWIFT_SHARED_LIBRARY_PATH);
const auto _Fibonacci = (FibonacciFunc)DLSymOrDie(swiftLib, SWIFT_FIBONACCI_FUNC_SYMBOL);
double arg0 = info[0]->NumberValue();
const auto n = _Fibonacci(arg0);
info.GetReturnValue().Set(n);
}
void PrintHello(const Nan::FunctionCallbackInfo<v8::Value>& info) {
const auto swiftLib = DLOpenOrDie(SWIFT_SHARED_LIBRARY_PATH);
const auto _PrintHello = (PrintHelloFunc)DLSymOrDie(swiftLib, SWIFT_PRINT_HELLO_FUNC_SYMBOL);
_PrintHello();
}
void Init(v8::Local<v8::Object> exports) {
exports->Set(Nan::New("fibonacci").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(Fibonacci)->GetFunction());
exports->Set(Nan::New("printHello").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(PrintHello)->GetFunction());
}
NODE_MODULE(addon, Init)