1
0
mirror of https://github.com/janet-lang/janet synced 2025-06-21 16:04:11 +00:00

Merge pull request #1 from janet-lang/master

Bringing fork up to date
This commit is contained in:
MikeBeller 2020-06-04 10:35:03 -04:00 committed by GitHub
commit 0b500730e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 125 additions and 48 deletions

View File

@ -122,6 +122,13 @@ gmake repl
3. Run `build_win` to compile janet. 3. Run `build_win` to compile janet.
4. Run `build_win test` to make sure everything is working. 4. Run `build_win test` to make sure everything is working.
To build an `.msi` installer executable, in addition to the above steps, you will have to:
5. Install, or otherwise add to your PATH the [WiX 3.11 Toolset](https://github.com/wixtoolset/wix3/releases)
6. run `build_win dist`
Now you should have an `.msi`. You can run `build_win install` to install the `.msi`, or execute the file itself.
### Meson ### Meson
Janet also has a build file for [Meson](https://mesonbuild.com/), a cross platform build Janet also has a build file for [Meson](https://mesonbuild.com/), a cross platform build

View File

@ -23,7 +23,7 @@ static int num_array_gc(void *p, size_t s) {
return 0; return 0;
} }
Janet num_array_get(void *p, Janet key); int num_array_get(void *p, Janet key, Janet *out);
void num_array_put(void *p, Janet key, Janet value); void num_array_put(void *p, Janet key, Janet value);
static const JanetAbstractType num_array_type = { static const JanetAbstractType num_array_type = {
@ -31,7 +31,8 @@ static const JanetAbstractType num_array_type = {
num_array_gc, num_array_gc,
NULL, NULL,
num_array_get, num_array_get,
num_array_put num_array_put,
JANET_ATEND_PUT
}; };
static Janet num_array_new(int32_t argc, Janet *argv) { static Janet num_array_new(int32_t argc, Janet *argv) {
@ -81,21 +82,20 @@ static const JanetMethod methods[] = {
{NULL, NULL} {NULL, NULL}
}; };
Janet num_array_get(void *p, Janet key) { int num_array_get(void *p, Janet key, Janet *out) {
size_t index; size_t index;
Janet value;
num_array *array = (num_array *)p; num_array *array = (num_array *)p;
if (janet_checktype(key, JANET_KEYWORD)) if (janet_checktype(key, JANET_KEYWORD))
return janet_getmethod(janet_unwrap_keyword(key), methods); return janet_getmethod(janet_unwrap_keyword(key), methods, out);
if (!janet_checkint(key)) if (!janet_checkint(key))
janet_panic("expected integer key"); janet_panic("expected integer key");
index = (size_t)janet_unwrap_integer(key); index = (size_t)janet_unwrap_integer(key);
if (index >= array->size) { if (index >= array->size) {
value = janet_wrap_nil(); return 0;
} else { } else {
value = janet_wrap_number(array->data[index]); *out = janet_wrap_number(array->data[index]);
} }
return value; return 1;
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {

View File

@ -0,0 +1,7 @@
(declare-project
:name "numarray"
:description "Example c lib with abstract type")
(declare-native
:name "numarray"
:source @["numarray.c"])

View File

@ -1,10 +1,4 @@
(import cook) (import build/numarray)
(cook/make-native
:name "numarray"
:source @["numarray.c"])
(import build/numarray :as numarray)
(def a (numarray/new 30)) (def a (numarray/new 30))
(print (get a 20)) (print (get a 20))

4
jpm
View File

@ -141,7 +141,9 @@
(if is-win (if is-win
(let [end (last (peg/match path-splitter src)) (let [end (last (peg/match path-splitter src))
isdir (= (os/stat src :mode) :directory)] isdir (= (os/stat src :mode) :directory)]
(shell "C:\\Windows\\System32\\xcopy.exe" src (if isdir (string dest "\\" end) dest) "/y" "/s" "/e" "/i")) (shell "C:\\Windows\\System32\\xcopy.exe"
(string/replace "/" "\\" src) (string/replace "/" "\\" (if isdir (string dest "\\" end) dest))
"/y" "/s" "/e" "/i"))
(shell "cp" "-rf" src dest))) (shell "cp" "-rf" src dest)))
(defn mkdir (defn mkdir

View File

@ -2017,19 +2017,26 @@
(while going (while going
(if (env :exit) (break)) (if (env :exit) (break))
(buffer/clear buf) (buffer/clear buf)
(chunks buf p) (if (= (chunks buf p)
(var pindex 0) :cancel)
(var pstatus nil) (do
(def len (length buf)) # A :cancel chunk represents a cancelled form in the REPL, so reset.
(when (= len 0) (parser/flush p)
(parser/eof p) (buffer/clear buf))
(set going false)) (do
(while (> len pindex) (var pindex 0)
(+= pindex (parser/consume p buf pindex)) (var pstatus nil)
(while (parser/has-more p) (def len (length buf))
(eval1 (parser/produce p))) (when (= len 0)
(when (= (parser/status p) :error) (parser/eof p)
(parse-err p where)))) (set going false))
(while (> len pindex)
(+= pindex (parser/consume p buf pindex))
(while (parser/has-more p)
(eval1 (parser/produce p)))
(when (= (parser/status p) :error)
(parse-err p where))))))
# Check final parser state # Check final parser state
(while (parser/has-more p) (while (parser/has-more p)
(eval1 (parser/produce p))) (eval1 (parser/produce p)))
@ -2600,7 +2607,9 @@
'def is-safe-def 'var is-safe-def 'def- is-safe-def 'var- is-safe-def 'def is-safe-def 'var is-safe-def 'def- is-safe-def 'var- is-safe-def
'defglobal is-safe-def 'varglobal is-safe-def}) 'defglobal is-safe-def 'varglobal is-safe-def})
(def- importers {'import true 'import* true 'use true 'dofile true 'require true}) (def- importers {'import true 'import* true 'dofile true 'require true})
(defn- use-2 [evaluator args]
(each a args (import* (string a) :prefix "" :evaluator evaluator)))
# conditional compilation for reduced os # conditional compilation for reduced os
(def- getenv-alias (if-let [entry (in _env 'os/getenv)] (entry :value) (fn [&]))) (def- getenv-alias (if-let [entry (in _env 'os/getenv)] (entry :value) (fn [&])))
@ -2688,6 +2697,9 @@
# Always safe form # Always safe form
safe-check safe-check
(thunk) (thunk)
# Use
(= 'use head)
(use-2 evaluator (tuple/slice source 1))
# Import-like form # Import-like form
(importers head) (importers head)
(do (do
@ -2738,6 +2750,7 @@
(put _env 'is-safe-def nil) (put _env 'is-safe-def nil)
(put _env 'safe-forms nil) (put _env 'safe-forms nil)
(put _env 'importers nil) (put _env 'importers nil)
(put _env 'use-2 nil)
(put _env 'getenv-alias nil) (put _env 'getenv-alias nil)
### ###

View File

@ -750,8 +750,10 @@ JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c) {
/* Copy upvalue bitset */ /* Copy upvalue bitset */
if (scope->ua.count) { if (scope->ua.count) {
/* Number of u32s we need to create a bitmask for all slots */ /* Number of u32s we need to create a bitmask for all slots */
int32_t numchunks = (def->slotcount + 31) >> 5; int32_t slotchunks = (def->slotcount + 31) >> 5;
uint32_t *chunks = malloc(sizeof(uint32_t) * numchunks); /* numchunks is min of slotchunks and scope->ua.count */
int32_t numchunks = slotchunks > scope->ua.count ? scope->ua.count : slotchunks;
uint32_t *chunks = calloc(sizeof(uint32_t), slotchunks);
if (NULL == chunks) { if (NULL == chunks) {
JANET_OUT_OF_MEMORY; JANET_OUT_OF_MEMORY;
} }

View File

@ -279,7 +279,10 @@ static Janet cfun_io_fclose(int32_t argc, Janet *argv) {
return janet_wrap_integer(WEXITSTATUS(status)); return janet_wrap_integer(WEXITSTATUS(status));
#endif #endif
} else { } else {
if (fclose(iof->file)) janet_panic("could not close file"); if (fclose(iof->file)) {
iof->flags |= JANET_FILE_NOT_CLOSEABLE;
janet_panic("could not close file");
}
iof->flags |= JANET_FILE_CLOSED; iof->flags |= JANET_FILE_CLOSED;
return janet_wrap_nil(); return janet_wrap_nil();
} }

View File

@ -26,6 +26,8 @@
#include "util.h" #include "util.h"
#endif #endif
#ifdef JANET_NET
#ifdef JANET_WINDOWS #ifdef JANET_WINDOWS
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
@ -675,3 +677,5 @@ void janet_net_deinit(void) {
WSACleanup(); WSACleanup();
#endif #endif
} }
#endif

View File

@ -1224,17 +1224,16 @@ static Janet os_rename(int32_t argc, Janet *argv) {
static Janet os_realpath(int32_t argc, Janet *argv) { static Janet os_realpath(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
#ifdef JANET_NO_REALPATH
(void) argv;
janet_panic("os/realpath not supported on this platform");
#else
const char *src = janet_getcstring(argv, 0); const char *src = janet_getcstring(argv, 0);
#ifdef JANET_WINDOWS
char *dest = _fullpath(NULL, src, _MAX_PATH);
#else
char *dest = realpath(src, NULL); char *dest = realpath(src, NULL);
#endif
if (NULL == dest) janet_panicf("%s: %s", strerror(errno), src); if (NULL == dest) janet_panicf("%s: %s", strerror(errno), src);
Janet ret = janet_cstringv(dest); Janet ret = janet_cstringv(dest);
free(dest); free(dest);
return ret; return ret;
#endif
} }
static Janet os_permission_string(int32_t argc, Janet *argv) { static Janet os_permission_string(int32_t argc, Janet *argv) {

View File

@ -150,6 +150,7 @@ tail:
down1(s); down1(s);
const uint8_t *result = peg_rule(s, s->bytecode + rule[2], text); const uint8_t *result = peg_rule(s, s->bytecode + rule[2], text);
up1(s); up1(s);
text -= ((int32_t *)rule)[1];
return result ? text : NULL; return result ? text : NULL;
} }

View File

@ -275,6 +275,24 @@ static void ta_setter(void *p, Janet key, Janet value) {
} }
} }
static Janet ta_view_next(void *p, Janet key) {
JanetTArrayView *view = p;
if (janet_checktype(key, JANET_NIL)) {
if (view->size > 0) {
return janet_wrap_number(0);
} else {
return janet_wrap_nil();
}
}
if (!janet_checksize(key)) janet_panic("expected size as key");
size_t index = (size_t) janet_unwrap_number(key);
index++;
if (index < view->size) {
return janet_wrap_number((double) index);
}
return janet_wrap_nil();
}
const JanetAbstractType janet_ta_view_type = { const JanetAbstractType janet_ta_view_type = {
"ta/view", "ta/view",
NULL, NULL,
@ -283,7 +301,11 @@ const JanetAbstractType janet_ta_view_type = {
ta_setter, ta_setter,
ta_view_marshal, ta_view_marshal,
ta_view_unmarshal, ta_view_unmarshal,
JANET_ATEND_UNMARSHAL NULL,
NULL,
NULL,
ta_view_next,
JANET_ATEND_NEXT
}; };
JanetTArrayBuffer *janet_tarray_buffer(size_t size) { JanetTArrayBuffer *janet_tarray_buffer(size_t size) {
@ -364,8 +386,11 @@ static Janet cfun_typed_array_new(int32_t argc, Janet *argv) {
offset = (view->buffer->data - view->as.u8) + offset * ta_type_sizes[view->type]; offset = (view->buffer->data - view->as.u8) + offset * ta_type_sizes[view->type];
stride *= view->stride; stride *= view->stride;
buffer = view->buffer; buffer = view->buffer;
} else { } else if (janet_abstract_type(p) == &janet_ta_buffer_type) {
buffer = p; buffer = p;
} else {
janet_panicf("bad slot #%d, expected ta/view|ta/buffer, got %v",
4, argv[4]);
} }
} }
JanetTArrayView *view = janet_tarray_view(type, size, stride, offset, buffer); JanetTArrayView *view = janet_tarray_view(type, size, stride, offset, buffer);

View File

@ -138,11 +138,6 @@ extern "C" {
#define JANET_NO_UTC_MKTIME #define JANET_NO_UTC_MKTIME
#endif #endif
/* Add some windows flags */
#ifdef JANET_WINDOWS
#define JANET_NO_REALPATH
#endif
/* Define how global janet state is declared */ /* Define how global janet state is declared */
#ifdef JANET_SINGLE_THREADED #ifdef JANET_SINGLE_THREADED
#define JANET_THREAD_LOCAL #define JANET_THREAD_LOCAL

View File

@ -40,6 +40,8 @@ void janet_line_deinit();
void janet_line_get(const char *p, JanetBuffer *buffer); void janet_line_get(const char *p, JanetBuffer *buffer);
Janet janet_line_getter(int32_t argc, Janet *argv); Janet janet_line_getter(int32_t argc, Janet *argv);
static JANET_THREAD_LOCAL int gbl_cancel_current_repl_form = 0;
/* /*
* Line Editing * Line Editing
*/ */
@ -54,7 +56,17 @@ Janet janet_line_getter(int32_t argc, Janet *argv) {
gbl_complete_env = (argc >= 3) ? janet_gettable(argv, 2) : NULL; gbl_complete_env = (argc >= 3) ? janet_gettable(argv, 2) : NULL;
janet_line_get(str, buf); janet_line_get(str, buf);
gbl_complete_env = NULL; gbl_complete_env = NULL;
return janet_wrap_buffer(buf);
Janet result;
if (gbl_cancel_current_repl_form) {
gbl_cancel_current_repl_form = 0;
/* Signal that the user bailed out of the current form */
result = janet_ckeywordv("cancel");
} else {
result = janet_wrap_buffer(buf);
}
return result;
} }
static void simpleline(JanetBuffer *buffer) { static void simpleline(JanetBuffer *buffer) {
@ -746,8 +758,7 @@ static int line() {
kleft(); kleft();
break; break;
case 3: /* ctrl-c */ case 3: /* ctrl-c */
errno = EAGAIN; gbl_cancel_current_repl_form = 1;
gbl_sigint_flag = 1;
clearlines(); clearlines();
return -1; return -1;
case 4: /* ctrl-d, eof */ case 4: /* ctrl-d, eof */

View File

@ -58,6 +58,17 @@
(assert (= ((unmarshal (marshal b)) 3) (b 3)) "marshal") (assert (= ((unmarshal (marshal b)) 3) (b 3)) "marshal")
# Issue 408
(assert-error :invalid-type (tarray/new :int32 10 1 0 (int/u64 7)) "tarray/new should only allow tarray or buffer for last argument")
(def ta (tarray/new :int32 10))
(assert (= (next a nil) 0) "tarray next 1")
(assert (= (next a 0) 1) "tarray next 2")
(assert (= (next a 8) 9) "tarray next 3")
(assert (nil? (next a 9)) "tarray next 4")
(put ta 3 7)
(put ta 9 7)
(assert (= 2 (count |(= $ 7) ta)) "tarray count")
# Array remove # Array remove
(assert (deep= (array/remove @[1 2 3 4 5] 2) @[1 2 4 5]) "array/remove 1") (assert (deep= (array/remove @[1 2 3 4 5] 2) @[1 2 4 5]) "array/remove 1")

View File

@ -285,4 +285,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
(assert (= nil (while (and false false) (fn []) (error "should not happen"))) "strangeloop 1") (assert (= nil (while (and false false) (fn []) (error "should not happen"))) "strangeloop 1")
(assert (= nil (while (not= nil nil) (fn []) (error "should not happen"))) "strangeloop 2") (assert (= nil (while (not= nil nil) (fn []) (error "should not happen"))) "strangeloop 2")
# Issue #412
(assert (peg/match '(* "a" (> -1 "a") "b") "abc") "lookhead does not move cursor")
(end-suite) (end-suite)