From cdd1a2bdc09105e9be0947c8c7990b1a7c737611 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Tue, 7 Jun 2022 22:41:27 -0700 Subject: Implement `hashCode` --- src/javascript/jsc/bindings/ZigGlobalObject.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index 7d745b896..3282370bd 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -137,7 +137,6 @@ using JSBuffer = WebCore::JSBuffer; #include "ReadableStream.h" // #include - static bool has_loaded_jsc = false; extern "C" void JSCInitialize() @@ -787,6 +786,21 @@ static JSC_DEFINE_HOST_FUNCTION(functionImportMeta__resolve, } } +static JSC_DECLARE_HOST_FUNCTION(functionHashCode); + +static JSC_DEFINE_HOST_FUNCTION(functionHashCode, + (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) +{ + JSC::JSValue stringToHash = callFrame->argument(0); + JSC::JSString* str = stringToHash.toStringOrNull(globalObject); + if (!str) { + return JSC::JSValue::encode(jsNumber(0)); + } + + auto view = str->value(globalObject); + return JSC::JSValue::encode(jsNumber(view.hash())); +} + static JSC_DECLARE_HOST_FUNCTION(functionImportMeta__resolveSync); static JSC_DEFINE_HOST_FUNCTION(functionImportMeta__resolveSync, @@ -1764,10 +1778,15 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm } { - JSC::Identifier identifier = JSC::Identifier::fromString(vm, "readableStreamToArray"_s); object->putDirectBuiltinFunction(vm, this, builtinNames.readableStreamToArrayPublicName(), readableStreamReadableStreamToArrayCodeGenerator(vm), PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); } + { + JSC::Identifier identifier = JSC::Identifier::fromString(vm, "hashCode"_s); + object->putDirectNativeFunction(vm, this, identifier, 1, functionHashCode, NoIntrinsic, + JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DontDelete | 0); + } + extraStaticGlobals.uncheckedAppend( GlobalPropertyInfo { JSC::Identifier::fromString(vm, jsClass->className()), JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 }); -- cgit v1.2.3