浏览代码

LibJS: Implement IsRegExp abstract operation

This is needed by various String.prototype operations, as well as
the RegExp constructor.
Xavier Cooney 4 年之前
父节点
当前提交
43f948b357
共有 2 个文件被更改,包括 17 次插入0 次删除
  1. 16 0
      Libraries/LibJS/Runtime/Value.cpp
  2. 1 0
      Libraries/LibJS/Runtime/Value.h

+ 16 - 0
Libraries/LibJS/Runtime/Value.cpp

@@ -102,6 +102,22 @@ Function& Value::as_function()
     return static_cast<Function&>(as_object());
     return static_cast<Function&>(as_object());
 }
 }
 
 
+bool Value::is_regexp(GlobalObject& global_object) const
+{
+    // 7.2.8 IsRegExp, https://tc39.es/ecma262/#sec-isregexp
+
+    if (!is_object())
+        return false;
+
+    auto matcher = as_object().get(global_object.vm().well_known_symbol_match());
+    if (global_object.vm().exception())
+        return false;
+    if (!matcher.is_empty() && !matcher.is_undefined())
+        return matcher.to_boolean();
+
+    return as_object().is_regexp_object();
+}
+
 String Value::to_string_without_side_effects() const
 String Value::to_string_without_side_effects() const
 {
 {
     switch (m_type) {
     switch (m_type) {

+ 1 - 0
Libraries/LibJS/Runtime/Value.h

@@ -76,6 +76,7 @@ public:
     bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol() || is_native_property(); }
     bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol() || is_native_property(); }
     bool is_array() const;
     bool is_array() const;
     bool is_function() const;
     bool is_function() const;
+    bool is_regexp(GlobalObject& global_object) const;
 
 
     bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
     bool is_nan() const { return is_number() && __builtin_isnan(as_double()); }
     bool is_infinity() const { return is_number() && __builtin_isinf(as_double()); }
     bool is_infinity() const { return is_number() && __builtin_isinf(as_double()); }