Skip to content

Commit

Permalink
test-url-is-url-internal
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jan 15, 2025
1 parent 834ad11 commit 6e496c6
Show file tree
Hide file tree
Showing 16 changed files with 906 additions and 134 deletions.
7 changes: 5 additions & 2 deletions src/bun.js/bindings/BunObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,15 +604,18 @@ JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObj
url = WTF::URL(arg0.toWTFString(globalObject));
RETURN_IF_EXCEPTION(scope, {});
} else {
throwTypeError(globalObject, scope, "Argument must be a URL"_s);
Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, "url"_s, "string"_s, arg0);
// throwTypeError(globalObject, scope, "Argument must be a URL"_s);
return {};
}
} else {
url = domURL->href();
}

if (UNLIKELY(!url.protocolIsFile())) {
throwTypeError(globalObject, scope, "Argument must be a file URL"_s);
// throwTypeError(globalObject, scope, "Argument must be a file URL"_s);
Bun::ERR::INVALID_URL_SCHEME(scope, globalObject, "file"_s);
// Bun::ERR:ErrorCode::ERR_INVALID_URL
return {};
}

Expand Down
7 changes: 7 additions & 0 deletions src/bun.js/bindings/ErrorCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,13 @@ JSC::EncodedJSValue INVALID_ARG_VALUE(JSC::ThrowScope& throwScope, JSC::JSGlobal
return {};
}

JSC::EncodedJSValue INVALID_URL_SCHEME(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, const WTF::String& expectedScheme)
{
auto message = makeString("The URL must be of scheme "_s, expectedScheme);
throwScope.throwException(globalObject, createError(globalObject, ErrorCode::ERR_INVALID_URL_SCHEME, message));
return {};
}

JSC::EncodedJSValue UNKNOWN_ENCODING(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, const WTF::StringView encoding)
{
auto message = makeString("Unknown encoding: "_s, encoding);
Expand Down
2 changes: 2 additions & 0 deletions src/bun.js/bindings/ErrorCode.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// To add a new error code, put it in ErrorCode.ts
#pragma once

#include "ZigGlobalObject.h"
Expand Down Expand Up @@ -79,6 +80,7 @@ JSC::EncodedJSValue OUT_OF_RANGE(JSC::ThrowScope& throwScope, JSC::JSGlobalObjec
JSC::EncodedJSValue INVALID_ARG_VALUE(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, WTF::ASCIILiteral name, JSC::JSValue value, const WTF::String& reason = "is invalid"_s);
JSC::EncodedJSValue INVALID_ARG_VALUE_RangeError(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, WTF::ASCIILiteral name, JSC::JSValue value, const WTF::String& reason = "is invalid"_s);
JSC::EncodedJSValue INVALID_ARG_VALUE(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, JSC::JSValue name, JSC::JSValue value, const WTF::String& reason = "is invalid"_s);
JSC::EncodedJSValue INVALID_URL_SCHEME(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, const WTF::String& expectedScheme);
JSC::EncodedJSValue UNKNOWN_ENCODING(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, const WTF::StringView encoding);
JSC::EncodedJSValue INVALID_STATE(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject, const WTF::String& statemsg);
JSC::EncodedJSValue STRING_TOO_LONG(JSC::ThrowScope& throwScope, JSC::JSGlobalObject* globalObject);
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default [
["MODULE_NOT_FOUND", Error],
["ERR_ILLEGAL_CONSTRUCTOR", TypeError],
["ERR_INVALID_URL", TypeError],
["ERR_INVALID_URL_SCHEME", TypeError],
["ERR_BUFFER_TOO_LARGE", RangeError],
["ERR_BROTLI_INVALID_PARAM", RangeError],
["ERR_UNKNOWN_ENCODING", TypeError],
Expand Down
26 changes: 26 additions & 0 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6308,3 +6308,29 @@ extern "C" EncodedJSValue Bun__JSObject__getCodePropertyVMInquiry(JSC::JSGlobalO

return JSValue::encode(slot.getPureResult());
}

using StackCodeType = JSC::StackVisitor::Frame::CodeType;
CPP_DECL bool Bun__util__isInsideNodeModules(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callFrame)
{
JSC::VM& vm = globalObject->vm();
bool inNodeModules = false;
JSC::StackVisitor::visit(callFrame, vm, [&](JSC::StackVisitor& visitor) -> WTF::IterationStatus {
if (Zig::isImplementationVisibilityPrivate(visitor) || visitor->isNativeCalleeFrame()) {
return WTF::IterationStatus::Continue;
}

if (visitor->hasLineAndColumnInfo()) {
String sourceURL = Zig::sourceURL(visitor);
if (sourceURL.startsWith("node:"_s) || sourceURL.startsWith("bun:"_s))
return WTF::IterationStatus::Continue;
if (sourceURL.startsWith("bun:"_s) || sourceURL.contains("node_modules"_s))
inNodeModules = true;

return WTF::IterationStatus::Done;
}

return WTF::IterationStatus::Continue;
});

return inNodeModules;
}
11 changes: 11 additions & 0 deletions src/bun.js/node/node_util_binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ pub fn extractedSplitNewLinesFastPathStringsOnly(globalThis: *JSC.JSGlobalObject
};
}

extern fn Bun__util__isInsideNodeModules(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) bool;
pub fn isInsideNodeModules(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue {
const res = Bun__util__isInsideNodeModules(globalObject, callframe);
return JSC.JSValue.jsBoolean(res);
}

// pub const isInsideNodeModules = Bun__util__isInsideNodeModules;
// pub fn isInsideNodeModules(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue {
// // globalThis.vm().
// const caller = callframe.getCallerSrcLoc();
// }
fn split(
comptime encoding: bun.strings.EncodingNonAscii,
globalThis: *JSC.JSGlobalObject,
Expand Down
1 change: 1 addition & 0 deletions src/codegen/generate-node-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let zig = ``;
enumHeader = `
// clang-format off
// Generated by: src/codegen/generate-node-errors.ts
// Input: src/bun.js/bindings/ErrorCode.ts
#pragma once
#include <cstdint>
Expand Down
8 changes: 8 additions & 0 deletions src/js/builtins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ declare function $getMapIteratorInternalField(): TODO;
declare function $getSetIteratorInternalField(): TODO;
declare function $getProxyInternalField(): TODO;
declare function $idWithProfile(): TODO;
/**
* True for `JSCell`s. That is, this is roughly equivalent to this JS code:
* ```js
* typeof obj === "object" && obj !== null
* ```
* @see [JSCell.h](https://github.com/oven-sh/WebKit/blob/main/Source/JavaScriptCore/runtime/JSCell.h)
* @see [JIT implementation](https://github.com/oven-sh/WebKit/blob/433f7598bf3537a295d0af5ffd83b9a307abec4e/Source/JavaScriptCore/jit/JITOpcodes.cpp#L311)
*/
declare function $isObject(obj: unknown): obj is object;
declare function $isArray(obj: unknown): obj is any[];
declare function $isCallable(fn: unknown): fn is CallableFunction;
Expand Down
5 changes: 5 additions & 0 deletions src/js/internal/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isInsideNodeModules: () => boolean = $newZigFunction("node_util_binding.zig", "isInsideNodeModules", 0);

export default {
isInsideNodeModules,
};
Loading

0 comments on commit 6e496c6

Please sign in to comment.