Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Forked from menangen/main.c
Created March 2, 2023 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickNaso/c390441cbfa6fc239c25fb1f2884787a to your computer and use it in GitHub Desktop.
Save NickNaso/c390441cbfa6fc239c25fb1f2884787a to your computer and use it in GitHub Desktop.
QuickJS C API
//
// 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");
}
//
// 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)
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