diff --git a/3p/include/llhs.h b/3p/include/llhs.h new file mode 100644 index 0000000..27d7019 --- /dev/null +++ b/3p/include/llhs.h @@ -0,0 +1,80 @@ + +#define MAX_HEADERS 16 +#define MAX_URI 4096 +#define MAX_BODY (1024 * 1024) +#define MAX_HNAME 128 +#define MAX_HBODY 1024 + +#define BUFFER_INIT_SIZE 256 +#define BUFFER_GROW_FACTOR 2 +#define BUFFER_GROW_ADDEND 0 + +#define BACKLOG 128 +#define POLL_TIMEOUT 5000 +#define MAX_POLL_EVENTS 64 +#define IMPLICIT_LENGTH 1 + +#define MAX_CLIENTS 256 +#define MAX_ALIVE 64 + +typedef void llhs_response; + +typedef enum { + llhs_HTTP_0_9, + llhs_HTTP_1_0, + llhs_HTTP_1_1, +} llhs_version; + +typedef enum { + llhs_GET, + llhs_PUT, + llhs_POST, + llhs_HEAD, + llhs_TRACE, + llhs_PATCH, + llhs_DELETE, + llhs_OPTIONS, + llhs_CONNECT, +} llhs_method; + +typedef struct { + const char *name; + const char *body; + int namel; + int bodyl; +} llhs_header; + +typedef struct { + llhs_version version; + llhs_method method; + const char *URI; + int URIl; + llhs_header headers[MAX_HEADERS]; + int headerc; + const void *body; + int bodyl; +} llhs_request; + +typedef struct { + _Bool occurred; + char message[256]; + int length; + const char *file, + *func; + int line; +} llhs_error; + +typedef void (*llhs_callback)(void *userp, const llhs_request *req, llhs_response *res); +typedef void (*llhs_logger )(void *userp, const char *level, const char *msg); + +_Bool llhs_serve(unsigned short port, void *userp, llhs_callback callback, llhs_logger logger, llhs_error *error); + +_Bool llhs_match (const char *x, const char *y); +_Bool llhs_match2(const char *x, const char *y, int xl, int yl); +const char *llhs_getheader(llhs_request *res, const char *name); + +void llhs_setcode (llhs_response *res, int code); +_Bool llhs_setbody (llhs_response *res, const void *body, int size); +void llhs_setbody2 (llhs_response *res, void *body, int bodyl, void (*free)(void *addr)); +_Bool llhs_setheader (llhs_response *res, const char *name, const char *body); +_Bool llhs_setheader2(llhs_response *res, const char *name, int body); diff --git a/3p/libs/libllhs.a b/3p/libs/libllhs.a new file mode 100644 index 0000000..167b5c2 Binary files /dev/null and b/3p/libs/libllhs.a differ diff --git a/samples/list.noja b/samples/list.noja index f23b286..ddb3efd 100644 --- a/samples/list.noja +++ b/samples/list.noja @@ -3,4 +3,11 @@ l = [1, 2, 3, 4]; print('The list contains ', count(l), ' items.\n'); -print('The list is: ', l, '.\n'); \ No newline at end of file +print('The list is: ', l, '.\n'); + + +fun fail() + assert(false); + + +fail(); \ No newline at end of file diff --git a/samples/webservice.noja b/samples/webservice.noja new file mode 100644 index 0000000..0940026 --- /dev/null +++ b/samples/webservice.noja @@ -0,0 +1,10 @@ + +fun index(req) + return {code: 200, 'body': 'Hello, world!'}; + +routes = { + '/': index, + '/groups': list_groups +} + +res = serve(8080, routes); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 5104b8c..9ed9750 100644 --- a/src/main.c +++ b/src/main.c @@ -217,7 +217,22 @@ int main(int argc, char **argv) RuntimeError error; RuntimeError_Init(&error, runtime); - + + Object *builtins = Object_NewBuiltinsMap(runtime, Runtime_GetHeap(runtime), (Error*) &error); + + if(builtins == NULL) + { + fprintf(stderr, "Couldn't initialize runtime.\n"); + Debug_Free(dbg); + Source_Free(src); + Executable_Free(exe); + RuntimeError_Free(&error); + Runtime_Free(runtime); + return 1; + } + + Runtime_SetBuiltins(runtime, builtins); + Object *result = run(runtime, (Error*) &error, exe, 0, NULL, 0); if(result == NULL) diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 08ca799..c262c12 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -101,17 +101,16 @@ void Runtime_Free(Runtime *runtime) free(runtime); } -Object *Runtime_GetBuiltins(Runtime *runtime, Error *error) +Object *Runtime_GetBuiltins(Runtime *runtime) { - if(runtime->builtins == NULL) - { - runtime->builtins = Object_NewBuiltinsMap(runtime, Runtime_GetHeap(runtime), error); - if(runtime->builtins == NULL) - return NULL; - } return runtime->builtins; } +void Runtime_SetBuiltins(Runtime *runtime, Object *builtins) +{ + runtime->builtins = builtins; +} + _Bool Runtime_Push(Runtime *runtime, Error *error, Object *obj) { assert(runtime != NULL); @@ -239,7 +238,56 @@ void Snapshot_Print(Snapshot *snapshot, FILE *fp) assert(snapshot != NULL); assert(fp != NULL); - fprintf(fp, " (Snapshot can't be printed yet)\n"); + fprintf(fp, "Stack trace:\n"); + + for(int i = 0; i < snapshot->depth; i += 1) + { + SnapshotNode node = snapshot->nodes[i]; + + Executable *exe = node.exe; + Source *src = Executable_GetSource(exe); + + const char *name; + { + name = NULL; + + if(src != NULL) + name = Source_GetName(src); + + if(name == NULL) + name = "(unnamed)"; + } + + int line; + { + if(src == NULL) + line = 0; + else + { + line = 1; + + const char *body = Source_GetBody(src); + int offset = Executable_GetInstrOffset(exe, node.index); + + int i = 0; + + while(i < offset) + { + if(body[i] == '\n') + line += 1; + + i += 1; + } + } + } + + if(line == 0) + fprintf(fp, "\t#%d %s\n", i, name); + else + fprintf(fp, "\t#%d %s:%d\n", i, name, line); + } + + //fprintf(fp, " (Snapshot can't be printed yet)\n"); } static Object *do_math_op(Object *lop, Object *rop, Opcode opcode, Heap *heap, Error *error) @@ -793,13 +841,10 @@ static _Bool step(Runtime *runtime, Error *error) { // Variable not defined locally. - Object *builtins = Runtime_GetBuiltins(runtime, error); + Object *builtins = Runtime_GetBuiltins(runtime); - if(builtins == NULL) - // Failed to create builtins map. - return 0; - - obj = Object_Select(runtime->builtins, key, runtime->heap, error); + if(builtins != NULL) + obj = Object_Select(builtins, key, runtime->heap, error); if(obj == NULL) { diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 1a2949a..406e289 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -13,7 +13,8 @@ _Bool Runtime_Pop(Runtime *runtime, Error *error, unsigned int n); _Bool Runtime_Push(Runtime *runtime, Error *error, Object *obj); Heap* Runtime_GetHeap(Runtime *runtime); Stack* Runtime_GetStack(Runtime *runtime); -Object* Runtime_GetBuiltins(Runtime *runtime, Error *error); +Object* Runtime_GetBuiltins(Runtime *runtime); +void Runtime_SetBuiltins(Runtime *runtime, Object *builtins); int Runtime_GetCurrentIndex(Runtime *runtime); Executable *Runtime_GetCurrentExecutable(Runtime *runtime);