wasm.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Language: WebAssembly
  3. Website: https://webassembly.org
  4. Description: Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.
  5. Category: web, common
  6. Audit: 2020
  7. */
  8. /** @type LanguageFn */
  9. function wasm(hljs) {
  10. hljs.regex;
  11. const BLOCK_COMMENT = hljs.COMMENT(/\(;/, /;\)/);
  12. BLOCK_COMMENT.contains.push("self");
  13. const LINE_COMMENT = hljs.COMMENT(/;;/, /$/);
  14. const KWS = [
  15. "anyfunc",
  16. "block",
  17. "br",
  18. "br_if",
  19. "br_table",
  20. "call",
  21. "call_indirect",
  22. "data",
  23. "drop",
  24. "elem",
  25. "else",
  26. "end",
  27. "export",
  28. "func",
  29. "global.get",
  30. "global.set",
  31. "local.get",
  32. "local.set",
  33. "local.tee",
  34. "get_global",
  35. "get_local",
  36. "global",
  37. "if",
  38. "import",
  39. "local",
  40. "loop",
  41. "memory",
  42. "memory.grow",
  43. "memory.size",
  44. "module",
  45. "mut",
  46. "nop",
  47. "offset",
  48. "param",
  49. "result",
  50. "return",
  51. "select",
  52. "set_global",
  53. "set_local",
  54. "start",
  55. "table",
  56. "tee_local",
  57. "then",
  58. "type",
  59. "unreachable"
  60. ];
  61. const FUNCTION_REFERENCE = {
  62. begin: [
  63. /(?:func|call|call_indirect)/,
  64. /\s+/,
  65. /\$[^\s)]+/
  66. ],
  67. className: {
  68. 1: "keyword",
  69. 3: "title.function"
  70. }
  71. };
  72. const ARGUMENT = {
  73. className: "variable",
  74. begin: /\$[\w_]+/
  75. };
  76. const PARENS = {
  77. match: /(\((?!;)|\))+/,
  78. className: "punctuation",
  79. relevance: 0
  80. };
  81. const NUMBER = {
  82. className: "number",
  83. relevance: 0,
  84. // borrowed from Prism, TODO: split out into variants
  85. match: /[+-]?\b(?:\d(?:_?\d)*(?:\.\d(?:_?\d)*)?(?:[eE][+-]?\d(?:_?\d)*)?|0x[\da-fA-F](?:_?[\da-fA-F])*(?:\.[\da-fA-F](?:_?[\da-fA-D])*)?(?:[pP][+-]?\d(?:_?\d)*)?)\b|\binf\b|\bnan(?::0x[\da-fA-F](?:_?[\da-fA-D])*)?\b/
  86. };
  87. const TYPE = {
  88. // look-ahead prevents us from gobbling up opcodes
  89. match: /(i32|i64|f32|f64)(?!\.)/,
  90. className: "type"
  91. };
  92. const MATH_OPERATIONS = {
  93. className: "keyword",
  94. // borrowed from Prism, TODO: split out into variants
  95. match: /\b(f32|f64|i32|i64)(?:\.(?:abs|add|and|ceil|clz|const|convert_[su]\/i(?:32|64)|copysign|ctz|demote\/f64|div(?:_[su])?|eqz?|extend_[su]\/i32|floor|ge(?:_[su])?|gt(?:_[su])?|le(?:_[su])?|load(?:(?:8|16|32)_[su])?|lt(?:_[su])?|max|min|mul|nearest|neg?|or|popcnt|promote\/f32|reinterpret\/[fi](?:32|64)|rem_[su]|rot[lr]|shl|shr_[su]|store(?:8|16|32)?|sqrt|sub|trunc(?:_[su]\/f(?:32|64))?|wrap\/i64|xor))\b/
  96. };
  97. const OFFSET_ALIGN = {
  98. match: [
  99. /(?:offset|align)/,
  100. /\s*/,
  101. /=/
  102. ],
  103. className: {
  104. 1: "keyword",
  105. 3: "operator"
  106. }
  107. };
  108. return {
  109. name: 'WebAssembly',
  110. keywords: {
  111. $pattern: /[\w.]+/,
  112. keyword: KWS
  113. },
  114. contains: [
  115. LINE_COMMENT,
  116. BLOCK_COMMENT,
  117. OFFSET_ALIGN,
  118. ARGUMENT,
  119. PARENS,
  120. FUNCTION_REFERENCE,
  121. hljs.QUOTE_STRING_MODE,
  122. TYPE,
  123. MATH_OPERATIONS,
  124. NUMBER
  125. ]
  126. };
  127. }
  128. export { wasm as default };