Skip to content

Commit

Permalink
json example
Browse files Browse the repository at this point in the history
  • Loading branch information
steeljustify committed Feb 2, 2025
1 parent 51545c2 commit 9f59922
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1,814 deletions.
8 changes: 0 additions & 8 deletions add.c

This file was deleted.

49 changes: 49 additions & 0 deletions fetch-json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "actrwasm.h"
#include "actrfetch.h"
#include "actrmap.h"
#include "actrcanvas.h"
#include "actrasync.h"
#include "actrjson.h"

// fetch json example

int asyncHandle = 0;
char * text = "loading will begin shortly...";

[[clang::export_name("actr_async_result")]]
void actr_async_result(int handle, int success)
{
if (handle == asyncHandle) {
if (success == 1) {
text = actr_json_get_string(1, "MyObject.one");
} else {
// this will happen if the fetch request fails
asyncHandle = 0;
text = "fetch error, retrying...";
}
}
}

[[clang::export_name("actr_step")]]
void actr_step(double delta)
{
if (asyncHandle == 0)
{
text = "loading...";
asyncHandle = actr_fetch_json("https://mrnathanstiles.github.io/test.json", 1);
if (asyncHandle < 1) {
// this would happen if the url is invalid or rejected
// this will happen if actr_async_result is not exported
text = "url error, retrying...";
asyncHandle = 0;
}
}


actr_canvas2d_fillStyle(0, 0, 0, 100);
actr_canvas2d_fillRect(-10, -10, 9999, 9999);

actr_canvas2d_fillStyle(255, 255, 255, 100);
actr_canvas2d_fillText(10, 20, text, strlen(text));

}
17 changes: 17 additions & 0 deletions header/actrjson.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
#include "actrwasm.h"

#ifndef ACTRJSON_H
#define ACTRJSON_H

int actr_json_get_int_length(int jsonId, char * path, int pathLength);
float actr_json_get_float_length(int jsonId, char * path, int pathLength);
char * actr_json_get_string_length(int jsonId, char * path, int pathLength);

int actr_json_get_int(int jsonId, char * path) {
return actr_json_get_int_length(jsonId, path, strlen(path));
}
float actr_json_get_float(int jsonId, char * path) {
return actr_json_get_float_length(jsonId, path, strlen(path));
}
char * actr_json_get_string(int jsonId, char * path) {
return actr_json_get_string_length(jsonId, path, strlen(path));
}

#endif

35 changes: 18 additions & 17 deletions pub.bat
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
@echo off

clear

SET script=%1
SET server=%2


if "%script%" == "" (
echo Example use
echo ./pub.bat scriptFile.c
exit
)

if "%script%" == "script.c" (
SET apiKey=pmu58grtf9vbyyxsy0ozr1l78wr1otlg4vaz7o1xpcz8amfd3x
)

if "%script%" == "fetch-text.c" (
SET apiKey=pjy2drrc8hhhvk9gx7c8ga3h3aa6l4en9rtk5sa8rbisgz0hpd
)


if "%script%" == "fetch-json.c" (
SET apiKey=w3pwm3k3bzdj9jskjzo8c5axm5ffd8ygz51tzgi6q9jgp98jjj
)

if "%script%" == "myScript" (
SET apiKey=myApiKey
)

if "%apiKey%" == "" (
echo apiKey not assigned for file
echo apiKey not assigned for file
exit
)

if %server% == local (
SET server=https://localhost:7086/api/Wasm/Upload/
) else (
SET server=https://www.d1ag0n.com/api/Wasm/Upload/
)

echo Building...

clang %script% -I header --target=wasm32-unknown-unknown ^
--optimize=3 -nostdlib -nostdlibinc -nostdinc -nostdinc++ ^
-Wl,--no-entry -Wl,--export-all -Wl,--error-limit=0 ^
-Wl,--allow-undefined --wasm-opt --output %script%.wasm

if %server% == local (
SET server=https://localhost:7086/api/Wasm/Upload/
echo SET LOCAL %server%
) else (
SET server=https://www.d1ag0n.com/api/Wasm/Upload/
echo SET REMOTE %server%
)
echo %server%


if %ERRORLEVEL% == 0 (
echo Build success uploading...

echo Uploading...
curl -k -F "data=@./%script%.wasm" "%server%%apiKey%%"
) else (
echo Build failed
Expand Down
201 changes: 0 additions & 201 deletions script.html

This file was deleted.

Loading

0 comments on commit 9f59922

Please sign in to comment.