| 1 | /* |
| 2 | * Copyright (C) 2017 Caio Lima <ticaiolima@gmail.com>. |
| 3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "BigIntPrototype.h" |
| 29 | |
| 30 | #include "BigIntObject.h" |
| 31 | #include "Error.h" |
| 32 | #include "JSBigInt.h" |
| 33 | #include "JSCBuiltins.h" |
| 34 | #include "JSCInlines.h" |
| 35 | #include "JSCast.h" |
| 36 | #include "JSFunction.h" |
| 37 | #include "JSGlobalObject.h" |
| 38 | #include "JSString.h" |
| 39 | #include "NumberPrototype.h" |
| 40 | #include <wtf/Assertions.h> |
| 41 | |
| 42 | namespace JSC { |
| 43 | |
| 44 | static EncodedJSValue JSC_HOST_CALL bigIntProtoFuncToString(ExecState*); |
| 45 | static EncodedJSValue JSC_HOST_CALL bigIntProtoFuncToLocaleString(ExecState*); |
| 46 | static EncodedJSValue JSC_HOST_CALL bigIntProtoFuncValueOf(ExecState*); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | #include "BigIntPrototype.lut.h" |
| 51 | |
| 52 | namespace JSC { |
| 53 | |
| 54 | const ClassInfo BigIntPrototype::s_info = { "BigInt" , &Base::s_info, &bigIntPrototypeTable, nullptr, CREATE_METHOD_TABLE(BigIntPrototype) }; |
| 55 | |
| 56 | /* Source for BigIntPrototype.lut.h |
| 57 | @begin bigIntPrototypeTable |
| 58 | toString bigIntProtoFuncToString DontEnum|Function 0 |
| 59 | toLocaleString bigIntProtoFuncToLocaleString DontEnum|Function 0 |
| 60 | valueOf bigIntProtoFuncValueOf DontEnum|Function 0 |
| 61 | @end |
| 62 | */ |
| 63 | |
| 64 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(BigIntPrototype); |
| 65 | |
| 66 | BigIntPrototype::BigIntPrototype(VM& vm, Structure* structure) |
| 67 | : JSNonFinalObject(vm, structure) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void BigIntPrototype::finishCreation(VM& vm, JSGlobalObject*) |
| 72 | { |
| 73 | Base::finishCreation(vm); |
| 74 | ASSERT(inherits(vm, info())); |
| 75 | putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "BigInt" ), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly); |
| 76 | } |
| 77 | |
| 78 | // ------------------------------ Functions --------------------------- |
| 79 | |
| 80 | static ALWAYS_INLINE JSBigInt* toThisBigIntValue(VM& vm, JSValue thisValue) |
| 81 | { |
| 82 | if (thisValue.isCell()) { |
| 83 | if (JSBigInt* bigInt = jsDynamicCast<JSBigInt*>(vm, thisValue.asCell())) |
| 84 | return bigInt; |
| 85 | |
| 86 | if (BigIntObject* bigIntObject = jsDynamicCast<BigIntObject*>(vm, thisValue.asCell())) |
| 87 | return bigIntObject->internalValue(); |
| 88 | } |
| 89 | |
| 90 | return nullptr; |
| 91 | } |
| 92 | |
| 93 | EncodedJSValue JSC_HOST_CALL bigIntProtoFuncToString(ExecState* state) |
| 94 | { |
| 95 | VM& vm = state->vm(); |
| 96 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 97 | |
| 98 | JSBigInt* value = toThisBigIntValue(vm, state->thisValue()); |
| 99 | if (!value) |
| 100 | return throwVMTypeError(state, scope, "'this' value must be a BigInt or BigIntObject"_s ); |
| 101 | |
| 102 | ASSERT(value); |
| 103 | |
| 104 | int32_t radix = extractToStringRadixArgument(state, state->argument(0), scope); |
| 105 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 106 | |
| 107 | String resultString = value->toString(state, radix); |
| 108 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 109 | scope.release(); |
| 110 | if (resultString.length() == 1) |
| 111 | return JSValue::encode(vm.smallStrings.singleCharacterString(resultString[0])); |
| 112 | |
| 113 | return JSValue::encode(jsNontrivialString(&vm, resultString)); |
| 114 | } |
| 115 | |
| 116 | EncodedJSValue JSC_HOST_CALL bigIntProtoFuncToLocaleString(ExecState* state) |
| 117 | { |
| 118 | return bigIntProtoFuncToString(state); |
| 119 | } |
| 120 | |
| 121 | EncodedJSValue JSC_HOST_CALL bigIntProtoFuncValueOf(ExecState* state) |
| 122 | { |
| 123 | VM& vm = state->vm(); |
| 124 | if (JSBigInt* value = toThisBigIntValue(vm, state->thisValue())) |
| 125 | return JSValue::encode(value); |
| 126 | |
| 127 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 128 | return throwVMTypeError(state, scope, "'this' value must be a BigInt or BigIntObject"_s ); |
| 129 | } |
| 130 | |
| 131 | } // namespace JSC |
| 132 | |