• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16"use strict";
17
18function deepEqual(x, y) {
19    // Approximate comparison for objects imported from ets, fails for some corner cases
20    if (x === y) {
21        return true;
22    } else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
23        if (Object.getPrototypeOf(x) !== Object.getPrototypeOf(y)) {
24            return false;
25        }
26        // Ets object fields are stored in prototype, but we cannot get them
27        // with Object.getPrototypeOf(x)[property]
28        for (let prop in x) {
29            if (Object.hasOwn(x, prop) != Object.hasOwn(y, prop)) {
30                return false;
31            }
32            if (!deepEqual(x[prop], y[prop])) {
33                return false;
34            }
35        }
36        // Check if y does not have properties not present in x
37        for (let prop in y) {
38            if (y[prop] !== undefined && x[prop] === undefined) {
39                return false;
40            }
41        }
42        return true;
43    } else {
44        return false;
45    }
46}
47exports.deepEqual = deepEqual;
48
49function JsClass(value) {
50    this.int_value = value;
51}
52
53let const_obj = new JsClass(1);
54function create_obj() {
55    return new JsClass(1);
56}
57function create_null_obj() {
58    return null;
59}
60
61% @type_infos.each do |type_info|
62% fn_name = "js_get_#{type_info.name}"
63exports.js_get_<%= type_info.name %> = function() {
64    return <%= type_info.value %>;
65}
66
67% end
68
69var saved_values = [undefined, undefined];
70
71exports.js_set = function(index, value) {
72    saved_values[index] = value;
73    return index;
74}
75
76exports.js_check_set = function(index, value) {
77    return deepEqual(saved_values[0], saved_values[1]) ? 0 : 1;
78}