1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-24 23:40:27 +00:00

Add example file for compiling native modules (hello.so).

This commit is contained in:
Calvin Rose 2018-04-26 13:13:31 -04:00
parent 8b9bd41205
commit 99709a68fd
10 changed files with 370 additions and 11 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
dst
!*/**/dst
/build
/Build
/Release
/Debug
/Emscripten

View File

@ -113,6 +113,7 @@ src/include/dst/dsttypes.h
# Build the executable
add_executable(${TARGET_NAME} ${REPL_SOURCES})
if (UNIX AND NOT EMSCRIPTEN)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -rdynamic")
target_link_libraries(${TARGET_NAME} m dl)
endif (UNIX AND NOT EMSCRIPTEN)
set_target_properties(${TARGET_NAME} PROPERTIES PUBLIC_HEADER "${DST_PUBLIC_HEADERS}")

View File

@ -25,7 +25,11 @@
PREFIX?=/usr/local
BINDIR=$(PREFIX)/bin
CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -s -O3
# CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -Wl,--dynamic-list=src/exported.list -s -O3
# TODO - when api is finalized, only export public symbols instead of using rdynamic
# which exports all symbols.
CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -rdynamic -s -O3
CLIBS=-lm -ldl
PREFIX=/usr/local
DST_TARGET=dst

32
natives/hello/Makefile Normal file
View File

@ -0,0 +1,32 @@
# Copyright (c) 2018 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.
CFLAGS=-std=c99 -Wall -Wextra -I../../src/include -s -O3
TARGET=hello.so
SOURCES=main.c
$(TARGET): $(SOURCES)
$(CC) $(CFLAGS) -shared -fpic -o $@ $<
clean:
rm $(TARGET)
.PHONY: clean

42
natives/hello/main.c Normal file
View File

@ -0,0 +1,42 @@
/*
* 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>
static int hello(DstArgs args) {
(void) args;
printf("Hello, world!\n");
return 0;
}
/*****************************************************************************/
static const DstReg cfuns[] = {
{"hello", hello},
{NULL, NULL}
};
int _dst_init(DstArgs args) {
DstTable *env = dst_env_arg(args);
dst_env_cfuns(env, cfuns);
return 0;
}

3
script.dst Normal file
View File

@ -0,0 +1,3 @@
(import "natives/hello/hello.so" :prefix "h/")
(h/hello)

View File

@ -917,15 +917,18 @@ onvalue."
(error (string "circular dependency: module " path " is loading")))
(def check (get cache path))
(if check check (do
(def newenv (make-env))
(put cache path newenv)
(put loading path true)
(def f (file-open path))
(defn chunks [buf] (file-read f 1024 buf))
(run-context newenv chunks identity default-error-handler)
(file-close f)
(put loading path nil)
newenv)))))
(if (= ".so" (string-slice path -3 -1))
((native path))
(do
(def newenv (make-env))
(put cache path newenv)
(put loading path true)
(def f (file-open path))
(defn chunks [buf] (file-read f 1024 buf))
(run-context newenv chunks identity default-error-handler)
(file-close f)
(put loading path nil)
newenv)))))))
(defn import* [env path & args]
(def newenv (require path))

View File

@ -37,7 +37,7 @@ typedef HINSTANCE Clib;
typedef int Clib;
#define load_clib(name) 0
#define symbol_clib(lib, sym) 0
#define error_clib() "could not load dynamic library"
#define error_clib() "dynamic libraries not supported"
#else
#include <dlfcn.h>
typedef void *Clib;

33
src/core/os.h Normal file
View File

@ -0,0 +1,33 @@
/*
* 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.
*/
#ifndef DST_OS_H
#define DST_OS_H
#include <dst/dst.h>
int dst_os_execute(DstArgs args);
int dst_os_getenv(DstArgs args);
int dst_os_setenv(DstArgs args);
int dst_os_exit(DstArgs args);
#endif

240
src/exported.list Normal file
View File

@ -0,0 +1,240 @@
{
dst_array;
dst_array_init;
dst_array_deinit;
dst_array_ensure;
dst_array_setcount;
dst_array_push;
dst_array_pop;
dst_array_peek;
dst_buffer;
dst_buffer_init;
dst_buffer_deinit;
dst_buffer_ensure;
dst_buffer_extra;
dst_buffer_push_bytes;
dst_buffer_push_cstring;
dst_buffer_push_u8;
dst_buffer_push_u16;
dst_buffer_push_u32;
dst_buffer_push_u64;
dst_tuple_begin;
dst_tuple_end;
dst_tuple_n;
dst_tuple_equal;
dst_tuple_compare;
dst_string_begin;
dst_string_end;
dst_string;
dst_cstring;
dst_string_compare;
dst_string_equal;
dst_string_equalconst;
dst_string_unique;
dst_cstring_unique;
dst_description;
dst_to_string;
dst_to_zerostring;
dst_formatc;
dst_puts;
dst_symbol;
dst_symbol_from_string;
dst_csymbol;
dst_symbol_gen;
dst_struct_begin;
dst_struct_put;
dst_struct_end;
dst_struct_get;
dst_struct_next;
dst_struct_to_table;
dst_struct_equal;
dst_struct_compare;
dst_struct_find;
dst_table;
dst_table_init;
dst_table_deinit;
dst_table_get;
dst_table_rawget;
dst_table_remove;
dst_table_put;
dst_table_next;
dst_table_to_struct;
dst_table_merge_table;
dst_table_merge_struct;
dst_table_find;
dst_fiber;
dst_seq_view;
dst_chararray_view;
dst_hashtable_view;
dst_abstract;
dst_native;
dst_mark;
dst_sweep;
dst_collect;
dst_clear_memory;
dst_gcroot;
dst_gcunroot;
dst_gcunrootall;
dst_gclock;
dst_gcunlock;
dst_funcdef_alloc;
dst_thunk;
dst_verify;
dst_quick_asm;
dst_equals;
dst_hash;
dst_compare;
dst_get;
dst_put;
dst_next;
dst_length;
dst_getindex;
dst_setindex;
dst_cstrcmp;
dst_init;
dst_deinit;
dst_run;
dst_resume;
dst_env_def;
dst_env_var;
dst_env_cfuns;
dst_env_resolve;
dst_env_arg;
dst_stl_env;
dst_asm;
dst_disasm;
dst_asm_decode_instruction;
dst_asm_cfun;
dst_disasm_cfun;
dst_lib_asm;
dst_compile;
dst_compile_cfun;
dst_lib_compile;
dst_stl_env;
dst_dobytes;
dst_dostring;
dst_core_native;
dst_int;
dst_real;
dst_add;
dst_subtract;
dst_multiply;
dst_divide;
dst_modulo;
dst_rand;
dst_srand;
dst_strict_equal;
dst_strict_notequal;
dst_ascending;
dst_descending;
dst_notdescending;
dst_notascending;
dst_numeric_eq;
dst_numeric_neq;
dst_numeric_gt;
dst_numeric_lt;
dst_numeric_gte;
dst_numeric_lte;
dst_bor;
dst_band;
dst_bxor;
dst_bnot;
dst_lshift;
dst_rshift;
dst_lshiftu;
dst_not;
dst_cos;
dst_sin;
dst_tan;
dst_acos;
dst_asin;
dst_atan;
dst_exp;
dst_log;
dst_log10;
dst_sqrt;
dst_floor;
dst_ceil;
dst_pow;
dst_core_print;
dst_core_describe;
dst_core_string;
dst_core_symbol;
dst_core_buffer;
dst_core_tuple;
dst_core_array;
dst_core_table;
dst_core_struct;
dst_core_buffer;
dst_core_gensym;
dst_core_length;
dst_core_get;
dst_core_rawget;
dst_core_getproto;
dst_core_setproto;
dst_core_put;
dst_core_type;
dst_core_next;
dst_core_hash;
dst_core_string_slice;
dst_core_gccollect;
dst_core_gcsetinterval;
dst_core_gcinterval;
dst_lib_io;
dst_lib_math;
dst_lib_array;
dst_lib_tuple;
dst_lib_buffer;
dst_lib_table;
dst_lib_fiber;
dst_lib_os;
dst_ast_wrap;
dst_ast_node;
dst_ast_unwrap1;
dst_ast_unwrap;
dst_scan_number;
dst_scan_integer;
dst_scan_real;
dst_parser_init;
dst_parser_deinit;
dst_parser_consume;
dst_parser_status;
dst_parser_produce;
dst_parser_error;
dst_parse_cfun;
dst_lib_parse;
};