-
-
Save NickNaso/c390441cbfa6fc239c25fb1f2884787a to your computer and use it in GitHub Desktop.
QuickJS C API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// testQJS | |
// | |
// Created by menangen on 15/08/2019. | |
// Copyright © 2019 menangen. All rights reserved. | |
// | |
#include "quickjs.h" | |
#include "quickjs-libc.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
JSRuntime* rt; JSContext* ctx; | |
rt = JS_NewRuntime(); | |
ctx = JS_NewContextRaw(rt); | |
JS_AddIntrinsicBaseObjects(ctx); | |
JS_AddIntrinsicEval(ctx); | |
JSValue global_obj, console; | |
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); | |
JS_FreeValue(ctx, global_obj); | |
const char source[] = "console.log(Math.pow(12, 2));100"; | |
size_t len = sizeof(source); | |
printf("Evaluate: %s length: %i\n", source, len); | |
JSValue ret = JS_Eval(ctx, | |
source, | |
len, | |
"<evalScript>", | |
JS_EVAL_TYPE_GLOBAL); | |
if (JS_IsException(ret)) { | |
js_std_dump_error(ctx); | |
JS_ResetUncatchableError(ctx); | |
} | |
JSValue d_JS = __JS_NewFloat64(ctx, 12.5); | |
int x = JS_VALUE_GET_INT(ret); | |
printf("Int: %i \n", x); | |
printf("Double: %f \n", JS_VALUE_GET_FLOAT64(d_JS)); | |
JSValue i_JS = JS_NewInt32(ctx, 1); | |
JS_ToInt32(ctx, &x, i_JS); | |
printf("1 Int: %i \n", x); | |
JS_FreeContext(ctx); | |
JS_FreeRuntime(rt); | |
printf("Hello, C!\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// swift-linux | |
// | |
// Created by menangen on 17.08.[14..19]. | |
// Copyright (c) 2014 menangen. All rights reserved. | |
// | |
import quickjs | |
func JSPrint(Context: OpaquePointer!, Value: JSValue, argc: Int32, argv: UnsafeMutablePointer<JSValue>!) -> JSValue { | |
print("Hello console from JS") | |
for index in 0..<argc { | |
let argument = argv[Int(index)] | |
let str = String(cString: JS_ToCString(Context, argument)) | |
print(str) | |
} | |
return JS_NewInt32(Context, 111) | |
} | |
let Runtime = JS_NewRuntime() | |
let Context = JS_NewContextRaw(Runtime) | |
JS_AddIntrinsicBaseObjects(Context) | |
JS_AddIntrinsicEval(Context) | |
let GlobalObject = JS_GetGlobalObject(Context) | |
let ConsoleObject = JS_NewObject(Context) | |
JS_SetPropertyStr(Context, | |
ConsoleObject, | |
"log", | |
JS_NewCFunction(Context, JSPrint, "log", 1)) | |
JS_SetPropertyStr(Context, GlobalObject, "console", ConsoleObject) | |
JS_FreeValue(Context, GlobalObject) | |
let JSSourceCode = "console.log(Math.pow(12, 2))" | |
let returnType = JS_Eval(Context, | |
JSSourceCode, | |
JSSourceCode.count, | |
"<evalScript>", | |
JS_EVAL_TYPE_GLOBAL) | |
JS_FreeContext(Context) | |
JS_FreeRuntime(Runtime) | |
print("End", returnType.u.int32) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static JSValue js_print(JSContext *ctx, JSValueConst this_val, | |
int argc, JSValueConst *argv) | |
{ | |
int i; | |
const char *str; | |
for(i = 0; i < argc; i++) { | |
if (i != 0) | |
putchar(' '); | |
str = JS_ToCString(ctx, argv[i]); | |
if (!str) | |
return JS_EXCEPTION; | |
fputs(str, stdout); | |
JS_FreeCString(ctx, str); | |
} | |
putchar('\n'); | |
return JS_UNDEFINED; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment