Added JS engine.

This commit is contained in:
2021-09-18 00:39:00 -07:00
parent 314f9a1aa7
commit 833ec5ae5f
52 changed files with 547 additions and 35712 deletions

13
src/script/api/api.c Normal file
View File

@ -0,0 +1,13 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "api.h"
void scriptApiAdd(scripter_t *scripter) {
scriptsApiIo(scripter);
scriptsApiPrimitive(scripter);
}

13
src/script/api/api.h Normal file
View File

@ -0,0 +1,13 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "primitive.h"
#include "io.h"
void scriptApiAdd(scripter_t *scripter);

1
src/script/api/global.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare type Pointer<T> = { 'POINTER':T };

24
src/script/api/io.c Normal file
View File

@ -0,0 +1,24 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "io.h"
scripterreturn_t _scriptPrint(scriptercontext_t *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_safe_to_string(ctx, -1));
return 0;
}
void scriptsApiIo(scripter_t *scripter) {
scripterDefineMethod(scripter,
SCRIPT_IO_PRINT_NAME,
SCRIPT_IO_PRINT_ARGS,
&_scriptPrint
);
}

1
src/script/api/io.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare function print(...args:any):void;

13
src/script/api/io.h Normal file
View File

@ -0,0 +1,13 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <dawn/dawn.h>
#include "../scripter.h"
#define SCRIPT_IO_PRINT_NAME "print"
#define SCRIPT_IO_PRINT_ARGS SCRIPTER_VARIABLE_ARGUMENT_COUNT
void scriptsApiIo(scripter_t *scripter);

View File

@ -0,0 +1,36 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "primitive.h"
scripterreturn_t _scriptPrimitiveDraw(scriptercontext_t *context) {
primitive_t *primitive = duk_to_pointer(context, 0);
int32_t start = duk_to_number(context, 1);
int32_t count = duk_to_number(context, 2);
primitiveDraw(primitive, start, count);
return 0;
}
scripterreturn_t _scriptCubeInit(scriptercontext_t *context) {
primitive_t *primitive = malloc(sizeof(primitive_t));
cubeInit(primitive, 1, 1, 1);
duk_push_pointer(context, primitive);
return 1;
}
void scriptsApiPrimitive(scripter_t *scripter) {
scripterDefineMethod(scripter,
SCRIPT_PRIMITIVE_DRAW_NAME,
SCRIPT_PRIMITIVE_DRAW_ARGS,
&_scriptPrimitiveDraw
);
scripterDefineMethod(scripter,
SCRIPT_CUBE_INIT_NAME,
SCRIPT_CUBE_INIT_ARGS,
&_scriptCubeInit
);
}

5
src/script/api/primitive.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare type PointerPrimitive = Pointer<'PRIMITIVE'>;
declare function primitiveDraw(primitive:PointerPrimitive, start:number, count:number);
declare function cubeCreate():PointerPrimitive;

View File

@ -0,0 +1,20 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../scripter.h"
#include "../../display/primitive.h"
#include "../../display/primitives/cube.h"
#define SCRIPT_PRIMITIVE_DRAW_NAME "primitiveDraw"
#define SCRIPT_PRIMITIVE_DRAW_ARGS 3
#define SCRIPT_CUBE_INIT_NAME "cubeCreate"
#define SCRIPT_CUBE_INIT_ARGS 0
void scriptsApiPrimitive(scripter_t *scripter);

73
src/script/scripter.c Normal file
View File

@ -0,0 +1,73 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scripter.h"
void scripterInit(scripter_t *scripter, engine_t *engine) {
scripter->context = duk_create_heap_default();
scripter->engine = engine;
// Global context is implied here?
// Push the script self reference
duk_push_pointer(scripter->context, scripter);
duk_put_global_string(scripter->context, SCRIPTER_SELF_NAME);
// Inject API
scriptApiAdd(scripter);
}
void scripterDispose(scripter_t *scripter) {
duk_destroy_heap(scripter->context);
}
scripter_t * scripterFromContext(scriptercontext_t *ctx) {
// Switch to global object
duk_push_global_object(ctx);
// Get the string in the object.
duk_get_prop_string(ctx, -1, SCRIPTER_SELF_NAME);
// Get the pointer from that string.
scripter_t *scripter = (scripter_t *)duk_get_pointer(ctx, -1);
// Pop the string.
duk_pop(ctx);
// Pop the global.
duk_pop(ctx);
return scripter;
}
void scripterDefineMethod(scripter_t *scripter,
char *name, int32_t argCount, scriptermethod_t *method
) {
duk_push_c_function(scripter->context, method, argCount);
duk_put_global_string(scripter->context, name);
}
bool scripterInvokeMethodSimple(scripter_t *scripter, char *method) {
// Push global
duk_push_global_object(scripter->context);
// Push method string
duk_get_prop_string(scripter->context, -1, method);
// Invoke string method
if(duk_pcall(scripter->context, 0) != 0) {
printf("Error: %s\n", duk_safe_to_string(scripter->context, -1));
return false;
}
// Pop result
duk_pop(scripter->context);
// Pop global
duk_pop(scripter->context);
return true;
}

54
src/script/scripter.h Normal file
View File

@ -0,0 +1,54 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "api/api.h"
/**
* Initialize the scripter engine.
*
* @param scripter Scripter engine to initialize.
* @param engine Game engine to use.
*/
void scripterInit(scripter_t *scripter, engine_t *engine);
/**
* Dispose a previously created scripter instance.
*
* @param scripter Scripter to dispose.
*/
void scripterDispose(scripter_t *scripter);
/**
* Retreive the scripter instance frm a scripter context.
*
* @param ctx Scripter context.
* @return Pointer to the scripter instance.
*/
scripter_t * scripterFromContext(scriptercontext_t *ctx);
/**
* Define a method onto the global scripter stack.
*
* @param scripter Scripter to define onto.
* @param name Name of the method.
* @param argCount Arguments that the method takes.
* @param method Pointer to the method to receive the callback.
*/
void scripterDefineMethod(scripter_t *scripter,
char *name, int32_t argCount, scriptermethod_t *method
);
/**
* Invoke a method (without arguments) off the global stack.
*
* @param scripter Scripter to invoke frmo
* @param method Method to invoke.
* @return True if successful, otherwise false.
*/
bool scripterInvokeMethodSimple(scripter_t *scripter, char *method);