/src/moddable/xs/sources/xsBoolean.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016-2017 Moddable Tech, Inc. |
3 | | * |
4 | | * This file is part of the Moddable SDK Runtime. |
5 | | * |
6 | | * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This file incorporates work covered by the following copyright and |
20 | | * permission notice: |
21 | | * |
22 | | * Copyright (C) 2010-2016 Marvell International Ltd. |
23 | | * Copyright (C) 2002-2010 Kinoma, Inc. |
24 | | * |
25 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
26 | | * you may not use this file except in compliance with the License. |
27 | | * You may obtain a copy of the License at |
28 | | * |
29 | | * http://www.apache.org/licenses/LICENSE-2.0 |
30 | | * |
31 | | * Unless required by applicable law or agreed to in writing, software |
32 | | * distributed under the License is distributed on an "AS IS" BASIS, |
33 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 | | * See the License for the specific language governing permissions and |
35 | | * limitations under the License. |
36 | | */ |
37 | | |
38 | | #include "xsAll.h" |
39 | | |
40 | | static txSlot* fxCheckBoolean(txMachine* the, txSlot* it); |
41 | | |
42 | | void fxBuildBoolean(txMachine* the) |
43 | 28.7k | { |
44 | 28.7k | txSlot* slot; |
45 | 28.7k | mxPush(mxObjectPrototype); |
46 | 28.7k | slot = fxLastProperty(the, fxNewBooleanInstance(the)); |
47 | 28.7k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Boolean_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG); |
48 | 28.7k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Boolean_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG); |
49 | 28.7k | mxBooleanPrototype = *the->stack; |
50 | 28.7k | slot = fxBuildHostConstructor(the, mxCallback(fx_Boolean), 1, mxID(_Boolean)); |
51 | 28.7k | mxBooleanConstructor = *the->stack; |
52 | 28.7k | mxPop(); |
53 | 28.7k | } |
54 | | |
55 | | txSlot* fxNewBooleanInstance(txMachine* the) |
56 | 1.81M | { |
57 | 1.81M | txSlot* instance; |
58 | 1.81M | instance = fxNewObjectInstance(the); |
59 | 1.81M | fxNextBooleanProperty(the, instance, 0, XS_NO_ID, XS_INTERNAL_FLAG); |
60 | 1.81M | return instance; |
61 | 1.81M | } |
62 | | |
63 | | void fx_Boolean(txMachine* the) |
64 | 7.89k | { |
65 | 7.89k | txBoolean value = (mxArgc > 0) ? fxToBoolean(the, mxArgv(0)) : 0; |
66 | 7.89k | if (mxIsUndefined(mxTarget)) { |
67 | 3.12k | mxResult->kind = XS_BOOLEAN_KIND; |
68 | 3.12k | mxResult->value.boolean = value; |
69 | 3.12k | } |
70 | 4.77k | else { |
71 | 4.77k | txSlot* instance; |
72 | 4.77k | mxPushSlot(mxTarget); |
73 | 4.77k | fxGetPrototypeFromConstructor(the, &mxBooleanPrototype); |
74 | 4.77k | instance = fxNewBooleanInstance(the); |
75 | 4.77k | instance->next->value.boolean = value; |
76 | 4.77k | mxPullSlot(mxResult); |
77 | 4.77k | } |
78 | 7.89k | } |
79 | | |
80 | | void fx_Boolean_prototype_toString(txMachine* the) |
81 | 794 | { |
82 | 794 | txSlot* slot = fxCheckBoolean(the, mxThis); |
83 | 794 | if (!slot) mxTypeError("this: not a boolean"); |
84 | 786 | mxResult->kind = slot->kind; |
85 | 786 | mxResult->value = slot->value; |
86 | 786 | fxToString(the, mxResult); |
87 | 786 | } |
88 | | |
89 | | void fx_Boolean_prototype_valueOf(txMachine* the) |
90 | 4.43k | { |
91 | 4.43k | txSlot* slot = fxCheckBoolean(the, mxThis); |
92 | 4.43k | if (!slot) mxTypeError("this: not a boolean"); |
93 | 4.17k | mxResult->kind = slot->kind; |
94 | 4.17k | mxResult->value = slot->value; |
95 | 4.17k | } |
96 | | |
97 | | txSlot* fxCheckBoolean(txMachine* the, txSlot* it) |
98 | 5.22k | { |
99 | 5.22k | txSlot* result = C_NULL; |
100 | 5.22k | if (it->kind == XS_BOOLEAN_KIND) |
101 | 1.01k | result = it; |
102 | 4.21k | else if (it->kind == XS_REFERENCE_KIND) { |
103 | 4.21k | txSlot* instance = it->value.reference; |
104 | 4.21k | it = instance->next; |
105 | 4.21k | if ((it) && (it->flag & XS_INTERNAL_FLAG) && (it->kind == XS_BOOLEAN_KIND)) |
106 | 3.94k | result = it; |
107 | 4.21k | } |
108 | 5.22k | return result; |
109 | 5.22k | } |