1/** 2 * Copyright (c) 2021-2025 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 16export function cons(value, tail) { 17 return { value: value, tail: tail }; 18} 19 20export function car(node) { 21 return node.value; 22} 23 24export function cdr(node) { 25 return node.tail; 26} 27 28export function sum(a, b) { 29 return a + b; 30} 31 32export class TreeNode { 33 value; 34 left; 35 right; 36 37 constructor(value, left, right) { 38 this.value = value; 39 this.left = left; 40 this.right = right; 41 } 42 43 sum() { 44 let res = this.value; 45 if (this.left) { 46 res += this.left.sum(); 47 } 48 if (this.right) { 49 res += this.right.sum(); 50 } 51 return res; 52 } 53} 54 55export function makeDynObject(x) { 56 this.v0 = { value: x }; 57} 58 59export function doNothing() {} 60 61export function makeSwappable(obj) { 62 obj.swap = function () { 63 let tmp = this.first; 64 this.first = this.second; 65 this.second = tmp; 66 }; 67} 68 69export class StaticClass { 70 static staticProperty = 10; 71 static staticMethod() { 72 return this.staticProperty + 100; 73 } 74} 75 76export function extractSquaredInt(obj) { 77 let x = obj.intValue; 78 return x * x; 79} 80 81function MakeObjectWithPrototype() { 82 this.overriddenValue = 4; 83 this.overriddenFunction = function () { 84 return 'overridden'; 85 }; 86} 87 88let o1 = new MakeObjectWithPrototype(); 89 90MakeObjectWithPrototype.prototype.overriddenValue = -1; 91MakeObjectWithPrototype.prototype.overriddenFunction = function () { 92 return 'should be overridden'; 93}; 94MakeObjectWithPrototype.prototype.prototypeValue = 5; 95MakeObjectWithPrototype.prototype.prototypeFunction = function () { 96 return 'prototype function'; 97}; 98 99export let dynStorage = { 100 str: 'abcd', 101 dbl: 1.9, 102 integer: 6, 103 boolTrue: true, 104 boolFalse: false, 105 verify: function () { 106 const isStringValid = Number(this.str === 'dcba'); 107 const isFloatValid = Number(Math.abs(this.dbl - 2.4) < Number.EPSILON); 108 const isIntegerValid = Number(this.integer === 31); 109 const isBooleanValid = Number(this.boolTrue === false); 110 return isStringValid + isFloatValid + isIntegerValid + isBooleanValid; 111 }, 112}; 113 114export let ObjectWithPrototype = MakeObjectWithPrototype; 115export let vundefined = undefined; 116