nativeFunctionMatcher.js 1.0 KB

123456789101112131415161718192021222324252627282930
  1. // Copyright (C) 2016 Michael Ficarra. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: Assert _NativeFunction_ Syntax
  5. info: |
  6. This regex makes a best-effort determination that the tested string matches
  7. the NativeFunction grammar production without requiring a correct tokeniser.
  8. NativeFunction :
  9. function _IdentifierName_ opt ( _FormalParameters_ ) { [ native code ] }
  10. ---*/
  11. const NATIVE_FUNCTION_RE = /\bfunction\b[\s\S]*\([\s\S]*\)[\s\S]*\{[\s\S]*\[[\s\S]*\bnative\b[\s\S]+\bcode\b[\s\S]*\][\s\S]*\}/;
  12. const assertToStringOrNativeFunction = function(fn, expected) {
  13. const actual = "" + fn;
  14. try {
  15. assert.sameValue(actual, expected);
  16. } catch (unused) {
  17. assertNativeFunction(fn, expected);
  18. }
  19. };
  20. const assertNativeFunction = function(fn, special) {
  21. const actual = "" + fn;
  22. assert(
  23. NATIVE_FUNCTION_RE.test(actual),
  24. "Conforms to NativeFunction Syntax: '" + actual + "'." + (special ? "(" + special + ")" : "")
  25. );
  26. };