Compare commits

...

7 Commits

Author SHA1 Message Date
Alexander Klingenbeck 2ef1a00038 Remove static flag on platforms other than windows 2023-08-02 20:23:35 +02:00
Alexander Klingenbeck a3ed6bdb2f Link to static winpthread 2023-08-02 13:19:42 +02:00
Alexander Klingenbeck d967c49c45 Merge branch 'main' of https://github.com/mode777/rayjs 2023-07-30 20:59:18 +02:00
Alexander Klingenbeck 77cebb3dce Add js helpers back in 2023-07-30 20:59:11 +02:00
Alexander Klingenbeck 0c3a54b309
Create LICENSE 2023-07-30 18:18:20 +02:00
Alexander Klingenbeck 1579be5d1d
Update readme.md 2023-07-30 18:10:49 +02:00
Alexander Klingenbeck 52695d8a99 Update readme 2023-07-30 18:08:27 +02:00
5 changed files with 108 additions and 4 deletions

View File

@ -1,5 +1,12 @@
cmake_minimum_required(VERSION 3.1)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
# Workaround for windows to prevent static linking of pthread
IF (WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static")
ENDIF()
project(rayjs)
message("=== Configure raylib ===")

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Alexander Klingenbeck
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.

View File

@ -14,7 +14,7 @@ rayjs is not a binding for NodeJS nor is it running in the browser (yet). It's c
* In-depth auto-complete with definitions for the whole API
## Getting started
1. Download the binary for your platform from the release section.
1. Download the binary for your platform from the [release section](https://github.com/mode777/rayjs/releases).
2. Unzip the executable to a folder and create a new text file in the same folder. Rename the file to `main.js`
3. Open the file with a text-editor (e.g. Notepad) and add the following code
```javascript
@ -102,7 +102,8 @@ rayjs comes with full auto-complete support in the form of the definitions file
]
}
}
```
```
After that put the `lib.raylib.d.ts` file in the same folder and optionally restart your IDE. Auto-complete should be working:
![](doc/auto-complete.png)
## Examples

View File

@ -241,6 +241,81 @@ JSModuleDef *js_module_loader(JSContext *ctx,
return m;
}
/* load and evaluate a file */
static JSValue js_loadScript(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
uint8_t *buf;
const char *filename;
JSValue ret;
size_t buf_len;
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
buf = js_load_file(ctx, &buf_len, filename);
if (!buf) {
JS_ThrowReferenceError(ctx, "could not load '%s'", filename);
JS_FreeCString(ctx, filename);
return JS_EXCEPTION;
}
ret = JS_Eval(ctx, (char *)buf, buf_len, filename,
JS_EVAL_TYPE_GLOBAL);
js_free(ctx, buf);
JS_FreeCString(ctx, filename);
return ret;
}
static JSValue js_print(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int i;
const char *str;
size_t len;
for(i = 0; i < argc; i++) {
if (i != 0)
putchar(' ');
str = JS_ToCStringLen(ctx, &len, argv[i]);
if (!str)
return JS_EXCEPTION;
fwrite(str, 1, len, stdout);
JS_FreeCString(ctx, str);
}
putchar('\n');
return JS_UNDEFINED;
}
void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
{
JSValue global_obj, console, args;
int i;
/* XXX: should these global definitions be enumerable? */
global_obj = JS_GetGlobalObject(ctx);
console = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, console, "log",
JS_NewCFunction(ctx, js_print, "log", 1));
JS_SetPropertyStr(ctx, global_obj, "console", console);
/* same methods as the mozilla JS shell */
if (argc >= 0) {
args = JS_NewArray(ctx);
for(i = 0; i < argc; i++) {
JS_SetPropertyUint32(ctx, args, i, JS_NewString(ctx, argv[i]));
}
JS_SetPropertyStr(ctx, global_obj, "scriptArgs", args);
}
JS_SetPropertyStr(ctx, global_obj, "print",
JS_NewCFunction(ctx, js_print, "print", 1));
JS_SetPropertyStr(ctx, global_obj, "__loadScript",
JS_NewCFunction(ctx, js_loadScript, "__loadScript", 1));
JS_FreeValue(ctx, global_obj);
}
static int js_run(int argc, char** argv){
TraceLog(LOG_INFO, "Starting QuickJS");
rt = JS_NewRuntime();
@ -259,7 +334,7 @@ static int js_run(int argc, char** argv){
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
//js_std_add_helpers(ctx, argc, argv);
js_std_add_helpers(ctx, argc, argv);
const char *str = "import * as rl from 'raylib'\n"
"for (const key in rl) { globalThis[key] = rl[key] }\n";

2
thirdparty/raylib vendored

@ -1 +1 @@
Subproject commit 44659b7ba8bd6d517d75fac8675ecd026f713240
Subproject commit d3ea64983212f7451a9cfbf644da8a5c43dbc706