aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/bindings/RegularExpression.cpp35
-rw-r--r--src/bun.js/bindings/RegularExpression.zig57
-rw-r--r--src/bun.js/test/jest.zig35
-rw-r--r--src/bun.js/test/test.zig0
4 files changed, 125 insertions, 2 deletions
diff --git a/src/bun.js/bindings/RegularExpression.cpp b/src/bun.js/bindings/RegularExpression.cpp
new file mode 100644
index 000000000..1c8df1bc0
--- /dev/null
+++ b/src/bun.js/bindings/RegularExpression.cpp
@@ -0,0 +1,35 @@
+#include "root.h"
+#include "headers-handwritten.h"
+#include "JavaScriptCore/RegularExpression.h"
+
+using namespace JSC;
+using namespace JSC::Yarr;
+
+extern "C" RegularExpression* Yarr__RegularExpression__init(BunString pattern, uint16_t flags)
+{
+ return new RegularExpression(Bun::toWTFString(pattern), OptionSet<Flags>(static_cast<Flags>(flags)));
+}
+extern "C" void Yarr__RegularExpression__deinit(RegularExpression* re)
+{
+ delete re;
+}
+extern "C" bool Yarr__RegularExpression__isValid(RegularExpression* re)
+{
+ return re->isValid();
+}
+extern "C" int Yarr__RegularExpression__matchedLength(RegularExpression* re)
+{
+ return re->matchedLength();
+}
+extern "C" int Yarr__RegularExpression__searchRev(RegularExpression* re, BunString string)
+{
+ return re->searchRev(Bun::toWTFString(string));
+}
+// extern "C" int Yarr__RegularExpression__match(RegularExpression* re, BunString string, int32_t start, int32_t* matchLength)
+// {
+// return re->match(Bun::toWTFString(string), start, matchLength);
+// }
+extern "C" int Yarr__RegularExpression__matches(RegularExpression* re, BunString string)
+{
+ return re->match(Bun::toWTFString(string), 0, 0);
+} \ No newline at end of file
diff --git a/src/bun.js/bindings/RegularExpression.zig b/src/bun.js/bindings/RegularExpression.zig
new file mode 100644
index 000000000..9c3da4fa2
--- /dev/null
+++ b/src/bun.js/bindings/RegularExpression.zig
@@ -0,0 +1,57 @@
+const bun = @import("root").bun;
+
+pub const RegularExpression = opaque {
+ pub const Flags = enum(u16) {
+ none = 0,
+
+ hasIndices = 1 << 0,
+ global = 1 << 1,
+ ignoreCase = 1 << 2,
+ multiline = 1 << 3,
+ dotAll = 1 << 4,
+ unicode = 1 << 5,
+ unicodeSets = 1 << 6,
+ sticky = 1 << 7,
+ };
+
+ extern fn Yarr__RegularExpression__init(pattern: bun.String, flags: u16) *RegularExpression;
+ extern fn Yarr__RegularExpression__deinit(pattern: *RegularExpression) void;
+ extern fn Yarr__RegularExpression__isValid(this: *RegularExpression) bool;
+ extern fn Yarr__RegularExpression__matchedLength(this: *RegularExpression) i32;
+ extern fn Yarr__RegularExpression__searchRev(this: *RegularExpression) i32;
+ extern fn Yarr__RegularExpression__matches(this: *RegularExpression, string: bun.String) i32;
+
+ pub inline fn init(pattern: bun.String, flags: Flags) !*RegularExpression {
+ var regex = Yarr__RegularExpression__init(pattern, @intFromEnum(flags));
+ if (!regex.isValid()) {
+ regex.deinit();
+ return error.InvalidRegex;
+ }
+ return regex;
+ }
+
+ pub inline fn isValid(this: *RegularExpression) bool {
+ return Yarr__RegularExpression__isValid(this);
+ }
+
+ // Reserving `match` for a full match result.
+ // pub inline fn match(this: *RegularExpression, str: bun.String, startFrom: i32) MatchResult {
+ // }
+
+ // Simple boolean matcher
+ pub inline fn matches(this: *RegularExpression, str: bun.String) bool {
+ return Yarr__RegularExpression__matches(this, str) > 0;
+ }
+
+ pub inline fn searchRev(this: *RegularExpression, str: bun.String) i32 {
+ return Yarr__RegularExpression__searchRev(this, str);
+ }
+
+ pub inline fn matchedLength(this: *RegularExpression) i32 {
+ return Yarr__RegularExpression__matchedLength(this);
+ }
+
+ pub inline fn deinit(this: *RegularExpression) void {
+ Yarr__RegularExpression__deinit(this);
+ }
+};
diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig
index 39116054e..aacf671ce 100644
--- a/src/bun.js/test/jest.zig
+++ b/src/bun.js/test/jest.zig
@@ -34,6 +34,7 @@ const FeatureFlags = @import("root").bun.FeatureFlags;
const ArrayBuffer = @import("../base.zig").ArrayBuffer;
const Properties = @import("../base.zig").Properties;
const getAllocator = @import("../base.zig").getAllocator;
+const RegularExpression = bun.RegularExpression;
const ZigString = JSC.ZigString;
const JSInternalPromise = JSC.JSInternalPromise;
@@ -96,6 +97,10 @@ pub const TestRunner = struct {
afterAll: std.ArrayListUnmanaged(JSC.JSValue) = .{},
} = .{},
+ // Used for --test-name-pattern to reduce allocations
+ filter_regex: ?*RegularExpression,
+ filter_buffer: MutableString,
+
pub const Drainer = JSC.AnyTask.New(TestRunner, drain);
pub fn onTestTimeout(timer: *bun.uws.Timer) callconv(.C) void {
@@ -1441,6 +1446,17 @@ pub const Result = union(TestRunner.Test.Status) {
}
};
+fn appendParentLabel(
+ buffer: *bun.MutableString,
+ parent: *DescribeScope,
+) !void {
+ if (parent.parent) |par| {
+ try appendParentLabel(buffer, par);
+ }
+ try buffer.append(parent.label);
+ try buffer.append(" ");
+}
+
inline fn createScope(
globalThis: *JSGlobalObject,
callframe: *CallFrame,
@@ -1516,11 +1532,26 @@ inline fn createScope(
return .zero;
}
- const is_skip = tag == .skip or
+ var is_skip = tag == .skip or
(tag == .todo and (function == .zero or !Jest.runner.?.run_todo)) or
(tag != .only and Jest.runner.?.only and parent.tag != .only);
+ var tag_to_use = tag;
if (is_test) {
+ if (!is_skip) {
+ if (Jest.runner.?.filter_regex) |regex| {
+ var buffer: bun.MutableString = Jest.runner.?.filter_buffer;
+ buffer.reset();
+ appendParentLabel(&buffer, parent) catch @panic("Bun ran out of memory while filtering tests");
+ buffer.append(label) catch unreachable;
+ var str = bun.String.fromBytes(buffer.toOwnedSliceLeaky());
+ is_skip = !regex.matches(str);
+ if (is_skip) {
+ tag_to_use = .skip;
+ }
+ }
+ }
+
if (is_skip) {
parent.skip_count += 1;
function.unprotect();
@@ -1531,7 +1562,7 @@ inline fn createScope(
parent.tests.append(allocator, TestScope{
.label = label,
.parent = parent,
- .tag = tag,
+ .tag = tag_to_use,
.callback = if (is_skip) .zero else function,
.timeout_millis = timeout_ms,
}) catch unreachable;
diff --git a/src/bun.js/test/test.zig b/src/bun.js/test/test.zig
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/bun.js/test/test.zig