From d30653189a29088da4c6c9d6cb2e7ac00746bd2e Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 17 Feb 2026 09:12:13 -0600 Subject: [PATCH] fix syntax --- fbx.c | 46 ++++---- gltf.c | 42 +++---- gltf.cm | 300 ++++++++++++++++++++++++++++------------------- obj.c | 40 +++---- tests/readout.ce | 78 +++++++----- 5 files changed, 290 insertions(+), 216 deletions(-) diff --git a/fbx.c b/fbx.c index c674f74..00ab21d 100644 --- a/fbx.c +++ b/fbx.c @@ -8,7 +8,7 @@ static JSValue make_float_array(JSContext *js, const double *arr, int count) { JSValue a = JS_NewArray(js); for (int i = 0; i < count; i++) - JS_SetPropertyUint32(js, a, i, JS_NewFloat64(js, arr[i])); + JS_SetPropertyNumber(js, a, i, JS_NewFloat64(js, arr[i])); return a; } @@ -16,7 +16,7 @@ static JSValue make_float_array_f(JSContext *js, const float *arr, int count) { JSValue a = JS_NewArray(js); for (int i = 0; i < count; i++) - JS_SetPropertyUint32(js, a, i, JS_NewFloat64(js, arr[i])); + JS_SetPropertyNumber(js, a, i, JS_NewFloat64(js, arr[i])); return a; } @@ -145,7 +145,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JSValue buf = JS_NewObject(js); JS_SetPropertyStr(js, buf, "blob", js_new_blob_stoned_copy(js, buffer_data, total_buffer_size)); JS_SetPropertyStr(js, buf, "byte_length", JS_NewInt64(js, total_buffer_size)); - JS_SetPropertyUint32(js, buffers_arr, 0, buf); + JS_SetPropertyNumber(js, buffers_arr, 0, buf); JS_SetPropertyStr(js, obj, "buffers", buffers_arr); // Create views @@ -159,7 +159,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, pos_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, pos_view, "usage", JS_NewString(js, "vertex")); int pos_view_idx = view_idx; - JS_SetPropertyUint32(js, views_arr, view_idx++, pos_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, pos_view); int norm_view_idx = -1; if (global_has_normals) { @@ -170,7 +170,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, norm_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, norm_view, "usage", JS_NewString(js, "vertex")); norm_view_idx = view_idx; - JS_SetPropertyUint32(js, views_arr, view_idx++, norm_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, norm_view); } int uv_view_idx = -1; @@ -182,7 +182,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, uv_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, uv_view, "usage", JS_NewString(js, "vertex")); uv_view_idx = view_idx; - JS_SetPropertyUint32(js, views_arr, view_idx++, uv_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, uv_view); } JSValue idx_view = JS_NewObject(js); @@ -192,7 +192,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, idx_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, idx_view, "usage", JS_NewString(js, "index")); int idx_view_idx = view_idx; - JS_SetPropertyUint32(js, views_arr, view_idx++, idx_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, idx_view); JS_SetPropertyStr(js, obj, "views", views_arr); @@ -217,7 +217,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, spa, "normalized", JS_FALSE); JS_SetPropertyStr(js, spa, "min", JS_NULL); JS_SetPropertyStr(js, spa, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, spa); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, spa); int mesh_norm_acc = -1; if (global_has_normals) { @@ -231,7 +231,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, sna, "normalized", JS_FALSE); JS_SetPropertyStr(js, sna, "min", JS_NULL); JS_SetPropertyStr(js, sna, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, sna); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, sna); } int mesh_uv_acc = -1; @@ -246,7 +246,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, sua, "normalized", JS_FALSE); JS_SetPropertyStr(js, sua, "min", JS_NULL); JS_SetPropertyStr(js, sua, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, sua); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, sua); } int mesh_idx_acc = acc_idx; @@ -259,7 +259,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, sia, "normalized", JS_FALSE); JS_SetPropertyStr(js, sia, "min", JS_NULL); JS_SetPropertyStr(js, sia, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, sia); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, sia); // Create mesh JSValue m = JS_NewObject(js); @@ -280,10 +280,10 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, prim, "indices", JS_NewInt32(js, mesh_idx_acc)); JS_SetPropertyStr(js, prim, "material", mesh->materials.count > 0 ? JS_NewInt32(js, mesh->materials.data[0]->typed_id) : JS_NULL); - JS_SetPropertyUint32(js, prims_arr, 0, prim); + JS_SetPropertyNumber(js, prims_arr, 0, prim); JS_SetPropertyStr(js, m, "primitives", prims_arr); - JS_SetPropertyUint32(js, meshes_arr, mi, m); + JS_SetPropertyNumber(js, meshes_arr, mi, m); } JS_SetPropertyStr(js, obj, "accessors", accessors_arr); @@ -323,7 +323,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, m, "alpha_cutoff", JS_NewFloat64(js, 0.5)); JS_SetPropertyStr(js, m, "double_sided", JS_FALSE); - JS_SetPropertyUint32(js, materials_arr, i, m); + JS_SetPropertyNumber(js, materials_arr, i, m); } JS_SetPropertyStr(js, obj, "materials", materials_arr); @@ -335,7 +335,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, im, "kind", JS_NewString(js, "uri")); JS_SetPropertyStr(js, im, "uri", tf->filename.length > 0 ? JS_NewString(js, tf->filename.data) : JS_NULL); JS_SetPropertyStr(js, im, "mime", JS_NULL); - JS_SetPropertyUint32(js, images_arr, i, im); + JS_SetPropertyNumber(js, images_arr, i, im); } JS_SetPropertyStr(js, obj, "images", images_arr); @@ -345,7 +345,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JSValue t = JS_NewObject(js); JS_SetPropertyStr(js, t, "image", tex->file_index != UFBX_NO_INDEX ? JS_NewInt32(js, tex->file_index) : JS_NULL); JS_SetPropertyStr(js, t, "sampler", JS_NULL); - JS_SetPropertyUint32(js, textures_arr, i, t); + JS_SetPropertyNumber(js, textures_arr, i, t); } JS_SetPropertyStr(js, obj, "textures", textures_arr); @@ -375,7 +375,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a // Find child node index for (size_t ni = 0; ni < scene->nodes.count; ni++) { if (scene->nodes.data[ni] == node->children.data[ci]) { - JS_SetPropertyUint32(js, children, ci, JS_NewInt32(js, ni)); + JS_SetPropertyNumber(js, children, ci, JS_NewInt32(js, ni)); break; } } @@ -391,7 +391,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, n, "scale", make_float_array(js, s, 3)); JS_SetPropertyStr(js, n, "skin", JS_NULL); - JS_SetPropertyUint32(js, nodes_arr, i, n); + JS_SetPropertyNumber(js, nodes_arr, i, n); } JS_SetPropertyStr(js, obj, "nodes", nodes_arr); @@ -402,12 +402,12 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a // Find root node index for (size_t i = 0; i < scene->nodes.count; i++) { if (scene->nodes.data[i] == scene->root_node) { - JS_SetPropertyUint32(js, scene_nodes, 0, JS_NewInt32(js, i)); + JS_SetPropertyNumber(js, scene_nodes, 0, JS_NewInt32(js, i)); break; } } JS_SetPropertyStr(js, scene_obj, "nodes", scene_nodes); - JS_SetPropertyUint32(js, scenes_arr, 0, scene_obj); + JS_SetPropertyNumber(js, scenes_arr, 0, scene_obj); JS_SetPropertyStr(js, obj, "scenes", scenes_arr); JS_SetPropertyStr(js, obj, "scene", JS_NewInt32(js, 0)); @@ -419,7 +419,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, a, "name", stack->element.name.length > 0 ? JS_NewString(js, stack->element.name.data) : JS_NULL); JS_SetPropertyStr(js, a, "samplers", JS_NewArray(js)); JS_SetPropertyStr(js, a, "channels", JS_NewArray(js)); - JS_SetPropertyUint32(js, anims_arr, ai, a); + JS_SetPropertyNumber(js, anims_arr, ai, a); } JS_SetPropertyStr(js, obj, "animations", anims_arr); @@ -436,7 +436,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a if (cluster->bone_node) { for (size_t ni = 0; ni < scene->nodes.count; ni++) { if (scene->nodes.data[ni] == cluster->bone_node) { - JS_SetPropertyUint32(js, joints, ci, JS_NewInt32(js, ni)); + JS_SetPropertyNumber(js, joints, ci, JS_NewInt32(js, ni)); break; } } @@ -446,7 +446,7 @@ JSValue js_fbx_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, s, "inverse_bind_matrices", JS_NULL); JS_SetPropertyStr(js, s, "skeleton", JS_NULL); - JS_SetPropertyUint32(js, skins_arr, i, s); + JS_SetPropertyNumber(js, skins_arr, i, s); } JS_SetPropertyStr(js, obj, "skins", skins_arr); diff --git a/gltf.c b/gltf.c index 592130b..0aece12 100644 --- a/gltf.c +++ b/gltf.c @@ -73,7 +73,7 @@ static JSValue make_float_array(JSContext *js, const float *arr, int count) { JSValue a = JS_NewArray(js); for (int i = 0; i < count; i++) - JS_SetPropertyUint32(js, a, i, JS_NewFloat64(js, arr[i])); + JS_SetPropertyNumber(js, a, i, JS_NewFloat64(js, arr[i])); return a; } @@ -124,7 +124,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * else JS_SetPropertyStr(js, b, "blob", JS_NULL); JS_SetPropertyStr(js, b, "byte_length", JS_NewInt64(js, buf->size)); - JS_SetPropertyUint32(js, buffers_arr, i, b); + JS_SetPropertyNumber(js, buffers_arr, i, b); } JS_SetPropertyStr(js, obj, "buffers", buffers_arr); @@ -141,7 +141,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * if (bv->type == cgltf_buffer_view_type_vertices) usage = "vertex"; else if (bv->type == cgltf_buffer_view_type_indices) usage = "index"; JS_SetPropertyStr(js, v, "usage", JS_NewString(js, usage)); - JS_SetPropertyUint32(js, views_arr, i, v); + JS_SetPropertyNumber(js, views_arr, i, v); } JS_SetPropertyStr(js, obj, "views", views_arr); @@ -171,7 +171,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * } else { JS_SetPropertyStr(js, a, "max", JS_NULL); } - JS_SetPropertyUint32(js, accessors_arr, i, a); + JS_SetPropertyNumber(js, accessors_arr, i, a); } JS_SetPropertyStr(js, obj, "accessors", accessors_arr); @@ -206,10 +206,10 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * else JS_SetPropertyStr(js, p, "material", JS_NULL); - JS_SetPropertyUint32(js, prims_arr, pi, p); + JS_SetPropertyNumber(js, prims_arr, pi, p); } JS_SetPropertyStr(js, m, "primitives", prims_arr); - JS_SetPropertyUint32(js, meshes_arr, mi, m); + JS_SetPropertyNumber(js, meshes_arr, mi, m); } JS_SetPropertyStr(js, obj, "meshes", meshes_arr); @@ -226,7 +226,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JS_SetPropertyStr(js, im, "uri", JS_NewString(js, img->uri)); } JS_SetPropertyStr(js, im, "mime", img->mime_type ? JS_NewString(js, img->mime_type) : JS_NULL); - JS_SetPropertyUint32(js, images_arr, i, im); + JS_SetPropertyNumber(js, images_arr, i, im); } JS_SetPropertyStr(js, obj, "images", images_arr); @@ -237,7 +237,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JSValue t = JS_NewObject(js); JS_SetPropertyStr(js, t, "image", tex->image ? JS_NewInt32(js, (int)(tex->image - data->images)) : JS_NULL); JS_SetPropertyStr(js, t, "sampler", tex->sampler ? JS_NewInt32(js, (int)(tex->sampler - data->samplers)) : JS_NULL); - JS_SetPropertyUint32(js, textures_arr, i, t); + JS_SetPropertyNumber(js, textures_arr, i, t); } JS_SetPropertyStr(js, obj, "textures", textures_arr); @@ -250,7 +250,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JS_SetPropertyStr(js, s, "mag_filter", JS_NewInt32(js, samp->mag_filter)); JS_SetPropertyStr(js, s, "wrap_s", JS_NewInt32(js, samp->wrap_s)); JS_SetPropertyStr(js, s, "wrap_t", JS_NewInt32(js, samp->wrap_t)); - JS_SetPropertyUint32(js, samplers_arr, i, s); + JS_SetPropertyNumber(js, samplers_arr, i, s); } JS_SetPropertyStr(js, obj, "samplers", samplers_arr); @@ -284,7 +284,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JS_SetPropertyStr(js, m, "double_sided", JS_NewBool(js, mat->double_sided)); JS_SetPropertyStr(js, m, "unlit", JS_NewBool(js, mat->unlit)); - JS_SetPropertyUint32(js, materials_arr, i, m); + JS_SetPropertyNumber(js, materials_arr, i, m); } JS_SetPropertyStr(js, obj, "materials", materials_arr); @@ -298,7 +298,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JSValue children = JS_NewArray(js); for (cgltf_size ci = 0; ci < node->children_count; ci++) - JS_SetPropertyUint32(js, children, ci, JS_NewInt32(js, (int)(node->children[ci] - data->nodes))); + JS_SetPropertyNumber(js, children, ci, JS_NewInt32(js, (int)(node->children[ci] - data->nodes))); JS_SetPropertyStr(js, n, "children", children); if (node->has_matrix) { @@ -314,7 +314,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * } JS_SetPropertyStr(js, n, "skin", node->skin ? JS_NewInt32(js, (int)(node->skin - data->skins)) : JS_NULL); - JS_SetPropertyUint32(js, nodes_arr, i, n); + JS_SetPropertyNumber(js, nodes_arr, i, n); } JS_SetPropertyStr(js, obj, "nodes", nodes_arr); @@ -325,9 +325,9 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JSValue s = JS_NewObject(js); JSValue snodes = JS_NewArray(js); for (cgltf_size ni = 0; ni < scene->nodes_count; ni++) - JS_SetPropertyUint32(js, snodes, ni, JS_NewInt32(js, (int)(scene->nodes[ni] - data->nodes))); + JS_SetPropertyNumber(js, snodes, ni, JS_NewInt32(js, (int)(scene->nodes[ni] - data->nodes))); JS_SetPropertyStr(js, s, "nodes", snodes); - JS_SetPropertyUint32(js, scenes_arr, i, s); + JS_SetPropertyNumber(js, scenes_arr, i, s); } JS_SetPropertyStr(js, obj, "scenes", scenes_arr); JS_SetPropertyStr(js, obj, "scene", data->scene ? JS_NewInt32(js, (int)(data->scene - data->scenes)) : JS_NULL); @@ -349,7 +349,7 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * if (samp->interpolation == cgltf_interpolation_type_step) interp = "STEP"; else if (samp->interpolation == cgltf_interpolation_type_cubic_spline) interp = "CUBICSPLINE"; JS_SetPropertyStr(js, s, "interpolation", JS_NewString(js, interp)); - JS_SetPropertyUint32(js, samplers, si, s); + JS_SetPropertyNumber(js, samplers, si, s); } JS_SetPropertyStr(js, a, "samplers", samplers); @@ -375,11 +375,11 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * } JS_SetPropertyStr(js, target, "path", JS_NewString(js, path)); JS_SetPropertyStr(js, c, "target", target); - JS_SetPropertyUint32(js, channels, ci, c); + JS_SetPropertyNumber(js, channels, ci, c); } JS_SetPropertyStr(js, a, "channels", channels); - JS_SetPropertyUint32(js, anims_arr, ai, a); + JS_SetPropertyNumber(js, anims_arr, ai, a); } JS_SetPropertyStr(js, obj, "animations", anims_arr); @@ -391,11 +391,11 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JS_SetPropertyStr(js, s, "name", skin->name ? JS_NewString(js, skin->name) : JS_NULL); JSValue joints = JS_NewArray(js); for (cgltf_size ji = 0; ji < skin->joints_count; ji++) - JS_SetPropertyUint32(js, joints, ji, JS_NewInt32(js, (int)(skin->joints[ji] - data->nodes))); + JS_SetPropertyNumber(js, joints, ji, JS_NewInt32(js, (int)(skin->joints[ji] - data->nodes))); JS_SetPropertyStr(js, s, "joints", joints); JS_SetPropertyStr(js, s, "inverse_bind_matrices", skin->inverse_bind_matrices ? JS_NewInt32(js, (int)(skin->inverse_bind_matrices - data->accessors)) : JS_NULL); JS_SetPropertyStr(js, s, "skeleton", skin->skeleton ? JS_NewInt32(js, (int)(skin->skeleton - data->nodes)) : JS_NULL); - JS_SetPropertyUint32(js, skins_arr, i, s); + JS_SetPropertyNumber(js, skins_arr, i, s); } JS_SetPropertyStr(js, obj, "skins", skins_arr); @@ -403,11 +403,11 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst * JSValue exts = JS_NewObject(js); JSValue used = JS_NewArray(js); for (cgltf_size i = 0; i < data->extensions_used_count; i++) - JS_SetPropertyUint32(js, used, i, JS_NewString(js, data->extensions_used[i])); + JS_SetPropertyNumber(js, used, i, JS_NewString(js, data->extensions_used[i])); JS_SetPropertyStr(js, exts, "used", used); JSValue required = JS_NewArray(js); for (cgltf_size i = 0; i < data->extensions_required_count; i++) - JS_SetPropertyUint32(js, required, i, JS_NewString(js, data->extensions_required[i])); + JS_SetPropertyNumber(js, required, i, JS_NewString(js, data->extensions_required[i])); JS_SetPropertyStr(js, exts, "required", required); JS_SetPropertyStr(js, obj, "extensions", exts); diff --git a/gltf.cm b/gltf.cm index 09946ae..a2fde6a 100644 --- a/gltf.cm +++ b/gltf.cm @@ -16,11 +16,11 @@ function dirname(path) { } function join_paths(base, rel) { - base = base.replace(/\/+$/, "") - rel = rel.replace(/^\/+/, "") - if (!base) return rel - if (!rel) return base - return base + "/" + rel + var _base = base.replace(/\/+$/, "") + var _rel = rel.replace(/^\/+/, "") + if (!_base) return _rel + if (!_rel) return _base + return _base + "/" + _rel } function ends_with(path, suffix) { @@ -29,13 +29,15 @@ function ends_with(path, suffix) { function spaces(count) { var s = "" - for (var i = 0; i < count; i++) s += " " + var i = 0 + for (i = 0; i < count; i++) s += " " return s } function zeros_blob(bytes) { var b = blob(bytes * 8) - for (var i = 0; i < bytes; i++) b.write_fit(0, 8) + var i = 0 + for (i = 0; i < bytes; i++) b.write_fit(0, 8) stone(b) return b } @@ -45,8 +47,9 @@ function u32(out, v) { } function warn(asset, msg, mode) { - if (mode == null) mode = "collect" - if (mode == "throw") throw Error(msg) + var _mode = mode + if (_mode == null) _mode = "collect" + if (_mode == "throw") disrupt asset.warnings.push(msg) } @@ -60,10 +63,12 @@ function parse_data_uri(uri) { var mime = "text/plain" var is_base64 = false + var parts = null + var i = 0 if (length(meta) > 0) { - var parts = array(meta, ";") + parts = array(meta, ";") if (parts[0]) mime = parts[0] - for (var i = 1; i < length(parts); i++) { + for (i = 1; i < length(parts); i++) { if (parts[i] == "base64") is_base64 = true } } @@ -73,16 +78,17 @@ function parse_data_uri(uri) { function load_uri_default(uri, base_dir) { if (uri.indexOf("://") != -1) return http.fetch(uri) - if (!base_dir) throw Error("missing base_dir for relative uri: " + uri) + if (!base_dir) disrupt return fd.slurp(join_paths(base_dir, uri)) } function load_uri(uri, base_dir, uri_loader) { if (uri == null) return null + var parsed = null if (starts_with(uri, "data:")) { - var parsed = parse_data_uri(uri) - if (!parsed) throw Error("invalid data uri") - if (!parsed.base64) throw Error("data uri is not base64") + parsed = parse_data_uri(uri) + if (!parsed) disrupt + if (!parsed.base64) disrupt return text.base64_to_blob(parsed.data) } return uri_loader(uri, base_dir) @@ -95,12 +101,12 @@ function parse_glb(glb_blob) { var version = glb_blob.read_fit(32, 32) var total_len = glb_blob.read_fit(64, 32) - if (magic != 0x46546c67) throw Error("invalid glb") - if (version != 2) throw Error("unsupported glb version") + if (magic != 0x46546c67) disrupt + if (version != 2) disrupt var json_len = glb_blob.read_fit(96, 32) var json_type = glb_blob.read_fit(128, 32) - if (json_type != 0x4e4f534a) throw Error("invalid glb json chunk") + if (json_type != 0x4e4f534a) disrupt var json_from = 160 var json_to = (20 + json_len) * 8 @@ -162,8 +168,10 @@ function make_glb_from_json_and_bin(doc, bin_blob) { function ensure_image_slots(asset) { if (!asset.images) asset.images = [] - for (var i = 0; i < length(asset.images); i++) { - var im = asset.images[i] + var i = 0 + var im = null + for (i = 0; i < length(asset.images); i++) { + im = asset.images[i] if (!im) continue if (im.encoded == null) im.encoded = null if (im.pixels == null) im.pixels = null @@ -171,11 +179,11 @@ function ensure_image_slots(asset) { } function synthesize_glb_from_gltf_json(doc, base_dir, uri_loader) { - if (!doc.buffers || length(doc.buffers) == 0) throw Error("gltf: .gltf has no buffers") - if (length(doc.buffers) != 1) throw Error("gltf: only .gltf with exactly 1 buffer is supported") + if (!doc.buffers || length(doc.buffers) == 0) disrupt + if (length(doc.buffers) != 1) disrupt var uri = doc.buffers[0].uri - if (!uri) throw Error("gltf: buffer[0] has no uri") + if (!uri) disrupt var bin_blob = load_uri(uri, base_dir, uri_loader) @@ -185,84 +193,96 @@ function synthesize_glb_from_gltf_json(doc, base_dir, uri_loader) { } gltf.decode = function(input, opts) { - if (opts == null) opts = {} + var _opts = opts + if (_opts == null) _opts = {} - var on_warning = opts.on_warning + var on_warning = _opts.on_warning if (on_warning == null) on_warning = "collect" - var uri_loader = opts.uri_loader + var uri_loader = _opts.uri_loader if (uri_loader == null) uri_loader = load_uri_default + var path = null + var gltf_blob = null + var doc = null + var base_dir = null + var glb = null + var asset = null + var glb_blob = null + var parse_fn = null if (is_text(input)) { - var path = input + path = input if (ends_with(path, ".gltf")) { - var gltf_blob = fd.slurp(path) - var doc = json.decode(text(gltf_blob)) - var base_dir = dirname(path) - var glb = synthesize_glb_from_gltf_json(doc, base_dir, uri_loader) - var asset = native_decode(glb) + gltf_blob = fd.slurp(path) + doc = json.decode(text(gltf_blob)) + base_dir = dirname(path) + glb = synthesize_glb_from_gltf_json(doc, base_dir, uri_loader) + asset = native_decode(glb) asset.warnings = [] asset.path = path asset.base_dir = base_dir asset.json = doc - asset.extensions = opts.extensions || {} + asset.extensions = _opts.extensions || {} ensure_image_slots(asset) return asset } - var glb_blob = fd.slurp(path) - var asset = native_decode(glb_blob) + glb_blob = fd.slurp(path) + asset = native_decode(glb_blob) asset.warnings = [] asset.path = path asset.base_dir = dirname(path) - asset.extensions = opts.extensions || {} - try { + asset.extensions = _opts.extensions || {} + parse_fn = function() { var parsed = parse_glb(glb_blob) asset.json = parsed.json asset.bin = parsed.bin - } catch (e) { - warn(asset, "gltf: could not parse glb json: " + e, on_warning) + } disruption { + warn(asset, "gltf: could not parse glb json", on_warning) asset.json = null asset.bin = null } + parse_fn() ensure_image_slots(asset) return asset } + var hint = null if (is_blob(input)) { - var hint = opts.path_hint + hint = _opts.path_hint if (hint && ends_with(hint, ".gltf")) { - var doc = json.decode(text(input)) - var base_dir = dirname(hint) - var glb = synthesize_glb_from_gltf_json(doc, base_dir, uri_loader) - var asset = native_decode(glb) + doc = json.decode(text(input)) + base_dir = dirname(hint) + glb = synthesize_glb_from_gltf_json(doc, base_dir, uri_loader) + asset = native_decode(glb) asset.warnings = [] asset.path = hint asset.base_dir = base_dir asset.json = doc - asset.extensions = opts.extensions || {} + asset.extensions = _opts.extensions || {} ensure_image_slots(asset) return asset } - var asset = native_decode(input) + asset = native_decode(input) asset.warnings = [] asset.path = hint || null asset.base_dir = hint ? dirname(hint) : null - asset.extensions = opts.extensions || {} - try { + asset.extensions = _opts.extensions || {} + parse_fn = function() { var parsed = parse_glb(input) asset.json = parsed.json asset.bin = parsed.bin - } catch (e) { + } disruption { asset.json = null asset.bin = null } + parse_fn() ensure_image_slots(asset) return asset } - throw Error("gltf.decode: input must be a string path or a blob") + disrupt } function used_texture_indices(asset) { @@ -274,8 +294,10 @@ function used_texture_indices(asset) { used[ti.texture] = true } - for (var i = 0; i < length(mats); i++) { - var m = mats[i] + var i = 0 + var m = null + for (i = 0; i < length(mats); i++) { + m = mats[i] if (!m) continue if (m.pbr) { mark_texinfo(m.pbr.base_color_texture) @@ -332,17 +354,20 @@ gltf.collect_dependencies = function(asset) { var doc = asset.json if (!doc) return deps + var i = 0 + var b = null if (doc.buffers) { - for (var i = 0; i < length(doc.buffers); i++) { - var b = doc.buffers[i] + for (i = 0; i < length(doc.buffers); i++) { + b = doc.buffers[i] if (b && b.uri) deps.buffers.push(b.uri) } } + var im2 = null if (doc.images) { - for (var i = 0; i < length(doc.images); i++) { - var im = doc.images[i] - if (im && im.uri) deps.images.push(im.uri) + for (i = 0; i < length(doc.images); i++) { + im2 = doc.images[i] + if (im2 && im2.uri) deps.images.push(im2.uri) } } @@ -350,21 +375,22 @@ gltf.collect_dependencies = function(asset) { } gltf.pull_images = function(asset, opts) { - if (opts == null) opts = {} + var _opts = opts + if (_opts == null) _opts = {} - var mode = opts.mode + var mode = _opts.mode if (mode == null) mode = "all" - var on_missing = opts.on_missing + var on_missing = _opts.on_missing if (on_missing == null) on_missing = "warn" - var uri_loader = opts.uri_loader + var uri_loader = _opts.uri_loader if (uri_loader == null) uri_loader = load_uri_default - var dedupe = opts.dedupe + var dedupe = _opts.dedupe if (dedupe == null) dedupe = true - var max_total_bytes = opts.max_total_bytes + var max_total_bytes = _opts.max_total_bytes if (max_total_bytes == null) max_total_bytes = 0 ensure_image_slots(asset) @@ -375,16 +401,31 @@ gltf.pull_images = function(asset, opts) { var cache = {} var total = 0 - for (var i = 0; i < length(asset.images); i++) { + var i = 0 + var img = null + var encoded = null + var key = null + var load_fn = function() { + encoded = load_uri(img.uri, asset.base_dir, uri_loader) + if (!stone.p(encoded)) stone(encoded) + img.encoded = encoded + if (dedupe) cache[key] = encoded + total += length(encoded) / 8 + if (max_total_bytes > 0 && total > max_total_bytes) disrupt + } disruption { + if (on_missing == "throw") disrupt + if (on_missing == "warn") warn(asset, "missing image uri: " + img.uri, "collect") + } + for (i = 0; i < length(asset.images); i++) { if (used && !used[i]) continue - var img = asset.images[i] + img = asset.images[i] if (!img) continue if (img.encoded) continue if (img.kind == "buffer_view") { - var encoded = extract_buffer_view(asset, img.view) + encoded = extract_buffer_view(asset, img.view) if (!encoded) { - if (on_missing == "throw") throw Error("missing embedded image bytes") + if (on_missing == "throw") disrupt if (on_missing == "warn") warn(asset, "missing embedded image bytes", "collect") continue } @@ -395,28 +436,18 @@ gltf.pull_images = function(asset, opts) { if (img.kind == "uri") { if (!img.uri) { - if (on_missing == "throw") throw Error("image has null uri") + if (on_missing == "throw") disrupt if (on_missing == "warn") warn(asset, "image has null uri", "collect") continue } - var key = img.uri + key = img.uri if (dedupe && cache[key]) { img.encoded = cache[key] continue } - try { - var encoded = load_uri(img.uri, asset.base_dir, uri_loader) - if (!stone.p(encoded)) stone(encoded) - img.encoded = encoded - if (dedupe) cache[key] = encoded - total += length(encoded) / 8 - if (max_total_bytes > 0 && total > max_total_bytes) throw Error("max_total_bytes exceeded") - } catch (e) { - if (on_missing == "throw") throw e - if (on_missing == "warn") warn(asset, "missing image uri: " + img.uri, "collect") - } + load_fn() continue } } @@ -426,8 +457,9 @@ gltf.pull_images = function(asset, opts) { function guess_mime(img) { if (img.mime) return img.mime + var u = null if (img.uri) { - var u = img.uri + u = img.uri if (ends_with(u, ".png")) return "image/png" if (ends_with(u, ".jpg") || ends_with(u, ".jpeg")) return "image/jpeg" } @@ -438,14 +470,22 @@ function premultiply_rgba(pixels_blob) { if (!stone.p(pixels_blob)) stone(pixels_blob) var bytes = length(pixels_blob) / 8 var out = blob(bytes * 8) - for (var i = 0; i < bytes; i += 4) { - var r = pixels_blob.read_fit((i + 0) * 8, 8) - var g = pixels_blob.read_fit((i + 1) * 8, 8) - var b = pixels_blob.read_fit((i + 2) * 8, 8) - var a = pixels_blob.read_fit((i + 3) * 8, 8) - var rr = floor((r * a) / 255) - var gg = floor((g * a) / 255) - var bb = floor((b * a) / 255) + var i = 0 + var r = null + var g = null + var b = null + var a = null + var rr = null + var gg = null + var bb = null + for (i = 0; i < bytes; i += 4) { + r = pixels_blob.read_fit((i + 0) * 8, 8) + g = pixels_blob.read_fit((i + 1) * 8, 8) + b = pixels_blob.read_fit((i + 2) * 8, 8) + a = pixels_blob.read_fit((i + 3) * 8, 8) + rr = floor((r * a) / 255) + gg = floor((g * a) / 255) + bb = floor((b * a) / 255) out.write_fit(rr, 8) out.write_fit(gg, 8) out.write_fit(bb, 8) @@ -456,25 +496,26 @@ function premultiply_rgba(pixels_blob) { } gltf.decode_images = function(asset, opts) { - if (opts == null) opts = {} + var _opts = opts + if (_opts == null) _opts = {} - var mode = opts.mode + var mode = _opts.mode if (mode == null) mode = "used" - var format = opts.format + var format = _opts.format if (format == null) format = "rgba8" - var on_missing_encoded = opts.on_missing_encoded + var on_missing_encoded = _opts.on_missing_encoded if (on_missing_encoded == null) on_missing_encoded = "pull" - var decoder = opts.decoder + var decoder = _opts.decoder if (decoder == null) decoder = function(mime, b) { if (mime == "image/png") return png.decode(b) if (mime == "image/jpeg") return jpg.decode(b) return null } - var premultiply_alpha = opts.premultiply_alpha + var premultiply_alpha = _opts.premultiply_alpha if (premultiply_alpha == null) premultiply_alpha = false ensure_image_slots(asset) @@ -486,29 +527,34 @@ gltf.decode_images = function(asset, opts) { var used = null if (mode == "used") used = used_image_indices(asset) - for (var i = 0; i < length(asset.images); i++) { + var i = 0 + var img = null + var mime = null + var decoded = null + var pixels = null + for (i = 0; i < length(asset.images); i++) { if (used && !used[i]) continue - var img = asset.images[i] + img = asset.images[i] if (!img) continue if (img.pixels) continue if (!img.encoded) { - if (on_missing_encoded == "throw") throw Error("missing encoded image data") + if (on_missing_encoded == "throw") disrupt continue } - var mime = guess_mime(img) + mime = guess_mime(img) if (!mime) { warn(asset, "unknown image mime", "collect") continue } - var decoded = decoder(mime, img.encoded) + decoded = decoder(mime, img.encoded) if (!decoded) { warn(asset, "unsupported image mime: " + mime, "collect") continue } - var pixels = decoded.pixels + pixels = decoded.pixels if (premultiply_alpha) pixels = premultiply_rgba(pixels) img.pixels = { @@ -524,14 +570,17 @@ gltf.decode_images = function(asset, opts) { } gltf.drop_images = function(asset, what) { - if (what == null) what = "all" + var _what = what + if (_what == null) _what = "all" ensure_image_slots(asset) - for (var i = 0; i < length(asset.images); i++) { - var img = asset.images[i] + var i = 0 + var img = null + for (i = 0; i < length(asset.images); i++) { + img = asset.images[i] if (!img) continue - if (what == "encoded" || what == "all") img.encoded = null - if (what == "pixels" || what == "all") img.pixels = null + if (_what == "encoded" || _what == "all") img.encoded = null + if (_what == "pixels" || _what == "all") img.pixels = null } return asset @@ -551,33 +600,41 @@ gltf.stats = function(asset) { triangles: 0 } + var i = 0 + var b = null + var im = null + var mi = 0 + var m = null + var pi = 0 + var p = null + var acc = null if (asset.buffers) { - for (var i = 0; i < length(asset.buffers); i++) { - var b = asset.buffers[i] + for (i = 0; i < length(asset.buffers); i++) { + b = asset.buffers[i] if (b && b.blob) stats.bin_bytes += length(b.blob) / 8 } } if (asset.images) { - for (var i = 0; i < length(asset.images); i++) { - var im = asset.images[i] + for (i = 0; i < length(asset.images); i++) { + im = asset.images[i] if (im && im.encoded) stats.encoded_image_bytes += length(im.encoded) / 8 } } if (asset.meshes) { - for (var mi = 0; mi < length(asset.meshes); mi++) { - var m = asset.meshes[mi] + for (mi = 0; mi < length(asset.meshes); mi++) { + m = asset.meshes[mi] if (!m || !m.primitives) continue - for (var pi = 0; pi < length(m.primitives); pi++) { - var p = m.primitives[pi] + for (pi = 0; pi < length(m.primitives); pi++) { + p = m.primitives[pi] if (!p) continue if (p.topology != "triangles") continue if (p.indices != null) { - var acc = asset.accessors[p.indices] + acc = asset.accessors[p.indices] if (acc) stats.triangles += floor((acc.count || 0) / 3) } else if (p.attributes && p.attributes.POSITION != null) { - var acc = asset.accessors[p.attributes.POSITION] + acc = asset.accessors[p.attributes.POSITION] if (acc) stats.triangles += floor((acc.count || 0) / 3) } } @@ -589,18 +646,19 @@ gltf.stats = function(asset) { } gltf.load = function(input, opts) { - if (opts == null) opts = {} + var _opts = opts + if (_opts == null) _opts = {} - var pull = opts.pull_images + var pull = _opts.pull_images if (pull == null) pull = true - var decode = opts.decode_images + var decode = _opts.decode_images if (decode == null) decode = false - var mode = opts.mode + var mode = _opts.mode if (mode == null) mode = "used" - var asset = gltf.decode(input, opts) + var asset = gltf.decode(input, _opts) if (pull) gltf.pull_images(asset, { mode: mode }) if (decode) gltf.decode_images(asset, { mode: mode }) return asset diff --git a/obj.c b/obj.c index df9625d..fbd201b 100644 --- a/obj.c +++ b/obj.c @@ -30,7 +30,7 @@ static JSValue make_float_array(JSContext *js, const float *arr, int count) { JSValue a = JS_NewArray(js); for (int i = 0; i < count; i++) - JS_SetPropertyUint32(js, a, i, JS_NewFloat64(js, arr[i])); + JS_SetPropertyNumber(js, a, i, JS_NewFloat64(js, arr[i])); return a; } @@ -160,7 +160,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JSValue buf = JS_NewObject(js); JS_SetPropertyStr(js, buf, "blob", js_new_blob_stoned_copy(js, buffer_data, total_buffer_size)); JS_SetPropertyStr(js, buf, "byte_length", JS_NewInt64(js, total_buffer_size)); - JS_SetPropertyUint32(js, buffers_arr, 0, buf); + JS_SetPropertyNumber(js, buffers_arr, 0, buf); JS_SetPropertyStr(js, obj, "buffers", buffers_arr); // Create views @@ -174,7 +174,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, pos_view, "byte_length", JS_NewInt64(js, pos_size)); JS_SetPropertyStr(js, pos_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, pos_view, "usage", JS_NewString(js, "vertex")); - JS_SetPropertyUint32(js, views_arr, view_idx++, pos_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, pos_view); int pos_view_idx = 0; int norm_view_idx = -1; @@ -186,7 +186,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, norm_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, norm_view, "usage", JS_NewString(js, "vertex")); norm_view_idx = view_idx; - JS_SetPropertyUint32(js, views_arr, view_idx++, norm_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, norm_view); } int uv_view_idx = -1; @@ -198,7 +198,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, uv_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, uv_view, "usage", JS_NewString(js, "vertex")); uv_view_idx = view_idx; - JS_SetPropertyUint32(js, views_arr, view_idx++, uv_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, uv_view); } // Index view @@ -209,7 +209,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, idx_view, "byte_stride", JS_NULL); JS_SetPropertyStr(js, idx_view, "usage", JS_NewString(js, "index")); int idx_view_idx = view_idx; - JS_SetPropertyUint32(js, views_arr, view_idx++, idx_view); + JS_SetPropertyNumber(js, views_arr, view_idx++, idx_view); JS_SetPropertyStr(js, obj, "views", views_arr); @@ -228,7 +228,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, pos_acc, "min", JS_NULL); JS_SetPropertyStr(js, pos_acc, "max", JS_NULL); int pos_acc_idx = acc_idx; - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, pos_acc); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, pos_acc); int norm_acc_idx = -1; if (global_has_normals) { @@ -242,7 +242,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, norm_acc, "min", JS_NULL); JS_SetPropertyStr(js, norm_acc, "max", JS_NULL); norm_acc_idx = acc_idx; - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, norm_acc); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, norm_acc); } int uv_acc_idx = -1; @@ -257,7 +257,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, uv_acc, "min", JS_NULL); JS_SetPropertyStr(js, uv_acc, "max", JS_NULL); uv_acc_idx = acc_idx; - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, uv_acc); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, uv_acc); } // Index accessor @@ -271,7 +271,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, idx_acc, "min", JS_NULL); JS_SetPropertyStr(js, idx_acc, "max", JS_NULL); int idx_acc_idx = acc_idx; - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, idx_acc); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, idx_acc); JS_SetPropertyStr(js, obj, "accessors", accessors_arr); @@ -302,7 +302,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, spa, "normalized", JS_FALSE); JS_SetPropertyStr(js, spa, "min", JS_NULL); JS_SetPropertyStr(js, spa, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, spa); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, spa); int shape_norm_acc = -1; if (global_has_normals) { @@ -316,7 +316,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, sna, "normalized", JS_FALSE); JS_SetPropertyStr(js, sna, "min", JS_NULL); JS_SetPropertyStr(js, sna, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, sna); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, sna); } int shape_uv_acc = -1; @@ -331,7 +331,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, sua, "normalized", JS_FALSE); JS_SetPropertyStr(js, sua, "min", JS_NULL); JS_SetPropertyStr(js, sua, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, sua); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, sua); } int shape_idx_acc = acc_idx; @@ -344,7 +344,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, sia, "normalized", JS_FALSE); JS_SetPropertyStr(js, sia, "min", JS_NULL); JS_SetPropertyStr(js, sia, "max", JS_NULL); - JS_SetPropertyUint32(js, accessors_arr, acc_idx++, sia); + JS_SetPropertyNumber(js, accessors_arr, acc_idx++, sia); // Create mesh JSValue mesh = JS_NewObject(js); @@ -365,10 +365,10 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, prim, "indices", JS_NewInt32(js, shape_idx_acc)); JS_SetPropertyStr(js, prim, "material", JS_NULL); - JS_SetPropertyUint32(js, prims_arr, 0, prim); + JS_SetPropertyNumber(js, prims_arr, 0, prim); JS_SetPropertyStr(js, mesh, "primitives", prims_arr); - JS_SetPropertyUint32(js, meshes_arr, si, mesh); + JS_SetPropertyNumber(js, meshes_arr, si, mesh); vertex_offset += shape_vertices; } @@ -400,7 +400,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, m, "alpha_cutoff", JS_NewFloat64(js, 0.5)); JS_SetPropertyStr(js, m, "double_sided", JS_FALSE); - JS_SetPropertyUint32(js, materials_arr, i, m); + JS_SetPropertyNumber(js, materials_arr, i, m); } JS_SetPropertyStr(js, obj, "materials", materials_arr); @@ -425,8 +425,8 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_SetPropertyStr(js, n, "rotation", make_float_array(js, r, 4)); JS_SetPropertyStr(js, n, "scale", make_float_array(js, s, 3)); JS_SetPropertyStr(js, n, "skin", JS_NULL); - JS_SetPropertyUint32(js, nodes_arr, i, n); - JS_SetPropertyUint32(js, scene_nodes, i, JS_NewInt32(js, i)); + JS_SetPropertyNumber(js, nodes_arr, i, n); + JS_SetPropertyNumber(js, scene_nodes, i, JS_NewInt32(js, i)); } JS_SetPropertyStr(js, obj, "nodes", nodes_arr); @@ -434,7 +434,7 @@ JSValue js_obj_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JSValue scenes_arr = JS_NewArray(js); JSValue scene = JS_NewObject(js); JS_SetPropertyStr(js, scene, "nodes", scene_nodes); - JS_SetPropertyUint32(js, scenes_arr, 0, scene); + JS_SetPropertyNumber(js, scenes_arr, 0, scene); JS_SetPropertyStr(js, obj, "scenes", scenes_arr); JS_SetPropertyStr(js, obj, "scene", JS_NewInt32(js, 0)); diff --git a/tests/readout.ce b/tests/readout.ce index 0de9585..1337523 100644 --- a/tests/readout.ce +++ b/tests/readout.ce @@ -37,8 +37,10 @@ def print_array = function(arr, indent) { log.console(`${indent}(empty)`) return } - for (var i = 0; i < length(arr); i++) { - var item = arr[i] + var i = 0 + var item = null + for (i = 0; i < length(arr); i++) { + item = arr[i] if (is_object(item)) { log.console(`${indent}[${i}]:`) print_object(item, indent + " ") @@ -51,40 +53,50 @@ def print_array = function(arr, indent) { log.console(`\n=== Model Structure: ${filepath} ===\n`) log.console(`Buffers: ${length(model.buffers)}`) -for (var i = 0; i < length(model.buffers); i++) { - var buf = model.buffers[i] +var i = 0 +var buf = null +for (i = 0; i < length(model.buffers); i++) { + buf = model.buffers[i] log.console(` [${i}] byte_length: ${buf.byte_length}`) } log.console(`\nViews: ${length(model.views)}`) -for (var i = 0; i < length(model.views); i++) { - var v = model.views[i] +var v = null +for (i = 0; i < length(model.views); i++) { + v = model.views[i] log.console(` [${i}] buffer: ${v.buffer}, offset: ${v.byte_offset}, length: ${v.byte_length}, stride: ${v.byte_stride}, usage: ${v.usage}`) } log.console(`\nAccessors: ${length(model.accessors)}`) -for (var i = 0; i < length(model.accessors); i++) { - var a = model.accessors[i] +var a = null +for (i = 0; i < length(model.accessors); i++) { + a = model.accessors[i] log.console(` [${i}] view: ${a.view}, offset: ${a.byte_offset}, count: ${a.count}, type: ${a.component_type}/${a.type}, normalized: ${a.normalized}`) } log.console(`\nMeshes: ${length(model.meshes)}`) -for (var i = 0; i < length(model.meshes); i++) { - var m = model.meshes[i] +var m = null +var j = 0 +var p = null +var ak = null +var parts = null +var k = 0 +for (i = 0; i < length(model.meshes); i++) { + m = model.meshes[i] log.console(` [${i}] name: ${m.name}`) - for (var j = 0; j < length(m.primitives); j++) { - var p = m.primitives[j] + for (j = 0; j < length(m.primitives); j++) { + p = m.primitives[j] log.console(` primitive[${j}]: topology: ${p.topology}, indices: ${p.indices}, material: ${p.material}`) - var ak = array(p.attributes) - var parts = [] - for (var k = 0; k < length(ak); k++) parts.push(ak[k] + ': ' + text(p.attributes[ak[k]])) + ak = array(p.attributes) + parts = [] + for (k = 0; k < length(ak); k++) parts.push(ak[k] + ': ' + text(p.attributes[ak[k]])) log.console(' attributes: {' + text(parts, ', ') + '}') } } log.console(`\nMaterials: ${length(model.materials)}`) -for (var i = 0; i < length(model.materials); i++) { - var m = model.materials[i] +for (i = 0; i < length(model.materials); i++) { + m = model.materials[i] log.console(` [${i}] name: ${m.name}, alpha_mode: ${m.alpha_mode}, double_sided: ${m.double_sided}`) if (m.pbr) { log.console(` pbr: base_color: [${m.pbr.base_color_factor}], metallic: ${m.pbr.metallic_factor}, roughness: ${m.pbr.roughness_factor}`) @@ -92,26 +104,30 @@ for (var i = 0; i < length(model.materials); i++) { } log.console(`\nImages: ${length(model.images)}`) -for (var i = 0; i < length(model.images); i++) { - var im = model.images[i] +var im = null +for (i = 0; i < length(model.images); i++) { + im = model.images[i] log.console(` [${i}] kind: ${im.kind}, ${im.kind == 'uri' ? 'uri: ' + im.uri : 'view: ' + im.view}, mime: ${im.mime}`) } log.console(`\nTextures: ${length(model.textures)}`) -for (var i = 0; i < length(model.textures); i++) { - var t = model.textures[i] +var t = null +for (i = 0; i < length(model.textures); i++) { + t = model.textures[i] log.console(` [${i}] image: ${t.image}, sampler: ${t.sampler}`) } log.console(`\nSamplers: ${length(model.samplers)}`) -for (var i = 0; i < length(model.samplers); i++) { - var s = model.samplers[i] +var s = null +for (i = 0; i < length(model.samplers); i++) { + s = model.samplers[i] log.console(` [${i}] min: ${s.min_filter}, mag: ${s.mag_filter}, wrap_s: ${s.wrap_s}, wrap_t: ${s.wrap_t}`) } log.console(`\nNodes: ${length(model.nodes)}`) -for (var i = 0; i < length(model.nodes); i++) { - var n = model.nodes[i] +var n = null +for (i = 0; i < length(model.nodes); i++) { + n = model.nodes[i] log.console(` [${i}] name: ${n.name}, mesh: ${n.mesh}, children: [${n.children}], skin: ${n.skin}`) if (n.matrix) { log.console(` matrix: [${array(n.matrix, 0, 4)}...]`) @@ -121,20 +137,20 @@ for (var i = 0; i < length(model.nodes); i++) { } log.console(`\nScenes: ${length(model.scenes)}, default: ${model.scene}`) -for (var i = 0; i < length(model.scenes); i++) { - var s = model.scenes[i] +for (i = 0; i < length(model.scenes); i++) { + s = model.scenes[i] log.console(` [${i}] nodes: [${s.nodes}]`) } log.console(`\nAnimations: ${length(model.animations)}`) -for (var i = 0; i < length(model.animations); i++) { - var a = model.animations[i] +for (i = 0; i < length(model.animations); i++) { + a = model.animations[i] log.console(` [${i}] name: ${a.name}, samplers: ${length(a.samplers)}, channels: ${length(a.channels)}`) } log.console(`\nSkins: ${length(model.skins)}`) -for (var i = 0; i < length(model.skins); i++) { - var s = model.skins[i] +for (i = 0; i < length(model.skins); i++) { + s = model.skins[i] log.console(` [${i}] name: ${s.name}, joints: ${length(s.joints)}, ibm: ${s.inverse_bind_matrices}, skeleton: ${s.skeleton}`) }