<commit_before>/* WASM specific WGPU demo scaffold functions */
#if !defined(__EMSCRIPTEN__)
#error "please compile this file in Emscripten!"
#endif

#include <stdio.h>
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include "wgpu_entry.h"

static struct {
    int frame_state;
} emsc;

static void emsc_update_canvas_size(void) {
    double w, h;
    emscripten_get_element_css_size("canvas", &w, &h);
    emscripten_set_canvas_element_size("canvas", w, h);
    wgpu_state.width = (int) w;
    wgpu_state.height = (int) h;
    printf("canvas size updated: %d %d\n", wgpu_state.width, wgpu_state.height);
}

static EM_BOOL emsc_size_changed(int event_type, const EmscriptenUiEvent* ui_event, void* user_data) {
    emsc_update_canvas_size();
    return true;
}

EMSCRIPTEN_KEEPALIVE void emsc_device_ready(int device_id, int swapchain_id, int swapchain_fmt) {
    wgpu_state.dev = (WGPUDevice) device_id;
    wgpu_state.swapchain = (WGPUSwapChain) swapchain_id;
    wgpu_state.swapchain_format = (WGPUTextureFormat) swapchain_fmt;
}

/* Javascript function to wrap asynchronous device and swapchain setup */
EM_JS(void, emsc_async_js_setup, (), {
    WebGPU.initManagers();
    navigator.gpu.requestAdapter().then(function(adapter) {
        adapter.requestDevice().then(function(device) {
            const gpuContext = document.getElementById("canvas").getContext("gpupresent");
            gpuContext.getSwapChainPreferredFormat(device).then(function(fmt) {
                const swapChainDescriptor = { device: device, format: fmt };
                const swapChain = gpuContext.configureSwapChain(swapChainDescriptor);
                var deviceId = WebGPU.mgrDevice.create(device);
                var swapChainId = WebGPU.mgrSwapChain.create(swapChain);
                var fmtId = WebGPU.TextureFormat.findIndex(function(elm) { return elm==fmt; });
                console.log("device: " + device);
                console.log("swap chain: " + swapChain);
                console.log("preferred format: " + fmt + " (" + fmtId + ")");
                _emsc_device_ready(deviceId, swapChainId, fmtId);
            });
        });
    });<commit_msg>wgpu glue: fix JS parts for closure compiler<commit_after>/* WASM specific WGPU demo scaffold functions */
#if !defined(__EMSCRIPTEN__)
#error "please compile this file in Emscripten!"
#endif

#include <stdio.h>
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include "wgpu_entry.h"

static struct {
    int frame_state;
} emsc;

static void emsc_update_canvas_size(void) {
    double w, h;
    emscripten_get_element_css_size("canvas", &w, &h);
    emscripten_set_canvas_element_size("canvas", w, h);
    wgpu_state.width = (int) w;
    wgpu_state.height = (int) h;
    printf("canvas size updated: %d %d\n", wgpu_state.width, wgpu_state.height);
}

static EM_BOOL emsc_size_changed(int event_type, const EmscriptenUiEvent* ui_event, void* user_data) {
    emsc_update_canvas_size();
    return true;
}

EMSCRIPTEN_KEEPALIVE void emsc_device_ready(int device_id, int swapchain_id, int swapchain_fmt) {
    wgpu_state.dev = (WGPUDevice) device_id;
    wgpu_state.swapchain = (WGPUSwapChain) swapchain_id;
    wgpu_state.swapchain_format = (WGPUTextureFormat) swapchain_fmt;
}

/* Javascript function to wrap asynchronous device and swapchain setup */
EM_JS(void, emsc_async_js_setup, (), {
    WebGPU.initManagers();
    navigator.gpu.requestAdapter().then(function(adapter) {
        adapter.requestDevice().then(function(device) {
            var gpuContext = document.getElementById("canvas").getContext("gpupresent");
            gpuContext.getSwapChainPreferredFormat(device).then(function(fmt) {
                var swapChainDescriptor = { device: device, format: fmt };
                var swapChain = gpuContext.configureSwapChain(swapChainDescriptor);
                var deviceId = WebGPU.mgrDevice.create(device);
                var swapChainId = WebGPU.mgrSwapChain.create(swapChain);
                var fmtId = WebGPU.TextureFormat.findIndex(function(elm) { return elm==fmt; });
                console.log("device: " + device);
                console.log("swap chain: " + swapChain);
                console.log("preferred format: " + fmt + " (" + fmtId + ")");
                _emsc_device_ready(deviceId, swapChainId, fmtId);
            });
        });
    });
{'fraction_non_alphanumeric': 0.11553420269961108, 'fraction_numerical': 0.0004575611988103409, 'mean_word_length': 3.525879917184265, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '775509', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1383, 'n_words': 275}