summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-bindgen-gecko-js/src/templates/js/Object.sys.mjs
blob: e03291089ee5571caefe2a53bb83aeea3ff23b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{%- let object = ci.get_object_definition(name).unwrap() -%}
export class {{ object.nm() }} {
    // Use `init` to instantiate this class.
    // DO NOT USE THIS CONSTRUCTOR DIRECTLY
    constructor(opts) {
        if (!Object.prototype.hasOwnProperty.call(opts, constructUniffiObject)) {
            throw new UniFFIError("Attempting to construct an object using the JavaScript constructor directly" +
            "Please use a UDL defined constructor, or the init function for the primary constructor")
        }
        if (!opts[constructUniffiObject] instanceof UniFFIPointer) {
            throw new UniFFIError("Attempting to create a UniFFI object with a pointer that is not an instance of UniFFIPointer")
        }
        this[uniffiObjectPtr] = opts[constructUniffiObject];
    }

    {%- for cons in object.constructors() %}
    {%- if object.is_constructor_async(config) %}
    /**
     * An async constructor for {{ object.nm() }}.
     * 
     * @returns {Promise<{{ object.nm() }}>}: A promise that resolves
     *      to a newly constructed {{ object.nm() }}
     */
    {%- else %}
    /**
     * A constructor for {{ object.nm() }}.
     * 
     * @returns { {{ object.nm() }} }
     */
    {%- endif %}
    static {{ cons.nm() }}({{cons.arg_names()}}) {
        {%- call js::call_constructor(cons, type_, object.is_constructor_async(config)) -%}
    }
    {%- endfor %}

    {%- for meth in object.methods() %}

    {{ meth.nm() }}({{ meth.arg_names() }}) {
        {%- call js::call_method(meth, type_, object.is_method_async(meth, config)) %}
    }
    {%- endfor %}

}

// Export the FFIConverter object to make external types work.
export class {{ ffi_converter }} extends FfiConverter {
    static lift(value) {
        const opts = {};
        opts[constructUniffiObject] = value;
        return new {{ object.nm() }}(opts);
    }

    static lower(value) {
        return value[uniffiObjectPtr];
    }

    static read(dataStream) {
        return this.lift(dataStream.readPointer{{ object.nm() }}());
    }

    static write(dataStream, value) {
        dataStream.writePointer{{ object.nm() }}(value[uniffiObjectPtr]);
    }

    static computeSize(value) {
        return 8;
    }
}