mirror of
https://github.com/janet-lang/janet
synced 2024-11-24 17:27:18 +00:00
Add untested code for loading dlls on windows.
This commit is contained in:
parent
4197f918a0
commit
53c0d5757c
2
Makefile
2
Makefile
@ -49,7 +49,7 @@ all: $(DST_TARGET)
|
||||
|
||||
DST_CORE_SOURCES=$(addprefix core/,\
|
||||
abstract.c array.c asm.c buffer.c compile.c compile_specials.c\
|
||||
fiber.c gc.c io.c math.c parse.c sourcemap.c string.c\
|
||||
fiber.c gc.c io.c math.c native.c parse.c sourcemap.c string.c\
|
||||
stl.c strtod.c struct.c symcache.c table.c tuple.c util.c\
|
||||
value.c vm.c wrap.c)
|
||||
|
||||
|
73
core/native.c
Normal file
73
core/native.c
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Calvin Rose
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <dst/dst.h>
|
||||
|
||||
/* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries
|
||||
* with native code. */
|
||||
#ifdef DST_WINDOWS
|
||||
#include <windows.h>
|
||||
typedef HINSTANCE Clib;
|
||||
#define load_clib(name) LoadLibrary((name))
|
||||
#define symbol_clib(lib, sym) GetProcAddress((lib), (sym))
|
||||
#define error_clib() "could not load dynamic library"
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
typedef void *Clib;
|
||||
#define load_clib(name) dlopen((name), RTLD_NOW)
|
||||
#define symbol_clib(lib, sym) dlsym((lib), (sym))
|
||||
#define error_clib() dlerror()
|
||||
#endif
|
||||
|
||||
DstCFunction dst_native(const char *name, const uint8_t **error) {
|
||||
Clib lib = load_clib(name);
|
||||
DstCFunction init;
|
||||
if (!lib) {
|
||||
*error = dst_cstring(error_clib());
|
||||
return NULL;
|
||||
}
|
||||
init = (DstCFunction) symbol_clib(lib, "_dst_init");
|
||||
if (!init) {
|
||||
*error = dst_cstring("could not find _dst_init symbol");
|
||||
return NULL;
|
||||
}
|
||||
return init;
|
||||
}
|
||||
|
||||
int dst_load_native(int32_t argn, Dst *argv, Dst *ret) {
|
||||
DstCFunction init;
|
||||
const uint8_t *error = NULL;
|
||||
if (argn < 1) {
|
||||
*ret = dst_cstringv("expected at least one argument");
|
||||
return 1;
|
||||
}
|
||||
if (!dst_checktype(argv[0], DST_STRING)) {
|
||||
*ret = dst_cstringv("expected string");
|
||||
return 1;
|
||||
}
|
||||
init = dst_native((const char *)dst_unwrap_string(argv[0]), &error);
|
||||
if (!init) {
|
||||
*ret = dst_wrap_string(error);
|
||||
return 1;
|
||||
}
|
||||
return init(argn, argv, ret);
|
||||
}
|
27
core/stl.c
27
core/stl.c
@ -23,31 +23,6 @@
|
||||
#include <dst/dst.h>
|
||||
#include <dst/dststl.h>
|
||||
|
||||
#include <dlfcn.h>
|
||||
int dst_stl_loadc(int32_t argn, Dst *argv, Dst *ret) {
|
||||
void *lib;
|
||||
DstCFunction init;
|
||||
if (argn < 1) {
|
||||
*ret = dst_cstringv("expected at least on argument");
|
||||
return 1;
|
||||
}
|
||||
if (!dst_checktype(argv[0], DST_STRING)) {
|
||||
*ret = dst_cstringv("expected string");
|
||||
return 1;
|
||||
}
|
||||
lib = dlopen((const char *)dst_unwrap_string(argv[0]), RTLD_NOW);
|
||||
if (NULL == lib) {
|
||||
*ret = dst_cstringv(dlerror());
|
||||
return 1;
|
||||
}
|
||||
init = dlsym(lib, "_dst_init");
|
||||
if (NULL == init) {
|
||||
*ret = dst_cstringv("could not find lib init function");
|
||||
return 1;
|
||||
}
|
||||
return init(argn, argv, ret);
|
||||
}
|
||||
|
||||
int dst_stl_parse(int32_t argn, Dst *argv, Dst *ret) {
|
||||
const uint8_t *src;
|
||||
int32_t len;
|
||||
@ -410,7 +385,7 @@ DST_DEFINE_COMPARATOR(notdescending, > 0)
|
||||
DST_DEFINE_COMPARATOR(notascending, < 0)
|
||||
|
||||
static DstReg stl[] = {
|
||||
{"load-native", dst_stl_loadc},
|
||||
{"load-native", dst_load_native},
|
||||
{"parse", dst_stl_parse},
|
||||
{"compile", dst_stl_compile},
|
||||
{"int", dst_int},
|
||||
|
@ -169,6 +169,10 @@ void dst_setindex(Dst ds, Dst value, int32_t index);
|
||||
DstParseResult dst_parse(const uint8_t *src, int32_t len);
|
||||
DstParseResult dst_parsec(const char *src);
|
||||
|
||||
/* Native */
|
||||
DstCFunction dst_native(const char *name, const uint8_t **error);
|
||||
int dst_load_native(int32_t argn, Dst *argv, Dst *ret);
|
||||
|
||||
/* VM functions */
|
||||
int dst_init();
|
||||
void dst_deinit();
|
||||
|
Loading…
Reference in New Issue
Block a user