|
| 1 | +(* Copyright (C) 2019- Authors of BuckleScript |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
| 24 | + |
| 25 | + type obj = Caml_obj_extern.t |
| 26 | + |
| 27 | + let setupChromeDebugger : unit -> unit = fun%raw unit -> {| |
| 28 | + |
| 29 | + // I don't know how to directly refer to the classes that chrome's built-in |
| 30 | + // formatters use. adding "class": "foo" doesn't seem to work |
| 31 | + // tree-outline |
| 32 | + var olStyle = {"style": "list-style-type: none; padding-left: 12px; margin: 0"} |
| 33 | + // object-properties-section-separator |
| 34 | + var colonStyle = {"style": "flex-shrink: 0; padding-right: 5px"} |
| 35 | + var recordNumberStyle = {"style": "flex-shrink: 0; padding-right: 5px; color: rgb(145, 145, 145)"} |
| 36 | + |
| 37 | + var showObject = function (value) { |
| 38 | + if (value == undefined) { |
| 39 | + return value + '' |
| 40 | + } else { |
| 41 | + return ["object", {"object": value}] |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + var recordCustomFormatter = function (data, labels) { |
| 46 | + return [ |
| 47 | + "ol", |
| 48 | + olStyle, |
| 49 | + ...data.map(function (cur, index) { |
| 50 | + return [ |
| 51 | + "li", |
| 52 | + {}, |
| 53 | + ["span", recordNumberStyle, index], |
| 54 | + ["span", {"style": "color: rgb(227, 110, 236)"}, labels[index]], |
| 55 | + ["span", colonStyle, ":"], |
| 56 | + ["span", {}, showObject(cur)], |
| 57 | + ] |
| 58 | + }) |
| 59 | + ] |
| 60 | + }; |
| 61 | + |
| 62 | +var listToArray = function (data){ |
| 63 | + var result = [] |
| 64 | + var cur = data |
| 65 | + var index = 0 |
| 66 | + while(typeof cur !== "number"){ |
| 67 | + result.push([ |
| 68 | + "li", |
| 69 | + {}, |
| 70 | + ["span", {"style": "color: rgb(227, 110, 236)"}, index], |
| 71 | + ["span", colonStyle, ":"], |
| 72 | + showObject(cur[0]) |
| 73 | + ]); |
| 74 | + cur = cur[1] |
| 75 | + index++ |
| 76 | + } |
| 77 | + return result |
| 78 | +}; |
| 79 | + |
| 80 | +var variantCustomFormatter = function (data,recordVariant){ |
| 81 | + if(recordVariant === "::"){ |
| 82 | + return [ |
| 83 | + "ol", |
| 84 | + olStyle, |
| 85 | + ... listToArray(data) |
| 86 | + ] |
| 87 | + } else { |
| 88 | + return ["ol", olStyle, ...data.map(function (cur) { return showObject(cur) })] |
| 89 | + } |
| 90 | + |
| 91 | +}; |
| 92 | + |
| 93 | +var recordPreview = function (recordLabels){ |
| 94 | + var recordLastIndex = recordLabels.length - 1 |
| 95 | + var preview = recordLabels.reduce(function (acc, cur, index) { |
| 96 | + if (index === recordLastIndex) { |
| 97 | + return acc + cur + "}" |
| 98 | + } |
| 99 | + return acc + cur + ", " |
| 100 | + }, "record {") |
| 101 | + return preview |
| 102 | +}; |
| 103 | + |
| 104 | +var variantPreview = function (x, recordVariant){ |
| 105 | + if(recordVariant === "::") { |
| 106 | + // show the length, just like for array |
| 107 | + var length = listToArray(x).length; |
| 108 | + return ['span', {}, `list(${length})`] |
| 109 | + } |
| 110 | + return ['span', {}, `${recordVariant}(…)`] |
| 111 | +}; |
| 112 | +var isOCamlExceptionOrExtensionHead = function(x){ |
| 113 | + return Array.isArray(x) && x.tag === 248 && typeof x[0] === "string" |
| 114 | +} |
| 115 | +var isOCamlExceptionOrExtension = function(x){ |
| 116 | + return Array.isArray(x) && |
| 117 | + x[0] !== undefined && |
| 118 | + isOCamlExceptionOrExtensionHead(x[0]) |
| 119 | +} |
| 120 | +var formatter = { |
| 121 | + header: function (x) { |
| 122 | + var recordLabels |
| 123 | + var recordVariant |
| 124 | + var recordModule |
| 125 | + var recordPolyVar |
| 126 | + if ((recordLabels = x[Symbol.for('BsRecord')]) !== undefined) { |
| 127 | + return ['div', {}, recordPreview(recordLabels)] |
| 128 | + } else if ((recordVariant = x[Symbol.for('BsVariant')]) !== undefined){ |
| 129 | + return variantPreview(x, recordVariant) |
| 130 | + } else if (isOCamlExceptionOrExtension(x)){ |
| 131 | + return ['div',{}, `${x[0][0]}(…)`] |
| 132 | + } else if ((recordModule = x[Symbol.for('BsLocalModule')]) !== undefined){ |
| 133 | + return ['div', {}, 'Module' ] |
| 134 | + } else if ((recordPolyVar = x[Symbol.for('BsPolyVar')] ) !== undefined){ |
| 135 | + return ['div', {}, `\`${recordPolyVar}#${x[0]}`] |
| 136 | + } |
| 137 | + return null |
| 138 | + }, |
| 139 | + hasBody: function (x) { |
| 140 | + var recordLabels |
| 141 | + var recordVariant |
| 142 | + var recordModule |
| 143 | + var recordPolyVar |
| 144 | + if ((recordLabels = x[Symbol.for('BsRecord')]) !== undefined) { |
| 145 | + return true |
| 146 | + } else if ((recordVariant = x[Symbol.for('BsVariant')] ) !== undefined){ |
| 147 | + return recordVariant |
| 148 | + } else if(isOCamlExceptionOrExtension(x)){ |
| 149 | + return true |
| 150 | + } else if ((recordModule = x[Symbol.for('BsLocalModule')] ) !== undefined){ |
| 151 | + return true |
| 152 | + } else if( (recordPolyVar = x[Symbol.for('BsPolyVar')]) !== undefined){ |
| 153 | + return true |
| 154 | + } |
| 155 | + return false |
| 156 | + }, |
| 157 | + body: function (x) { |
| 158 | + var recordLabels |
| 159 | + var recordVariant |
| 160 | + var recordModule |
| 161 | + var recordPolyVar |
| 162 | + if ( (recordLabels = x[Symbol.for('BsRecord')]) !== undefined |
| 163 | + ) { |
| 164 | + return recordCustomFormatter(x, recordLabels) |
| 165 | + } |
| 166 | + else if ((recordModule = x[Symbol.for('BsLocalModule')]) !== undefined){ |
| 167 | + return recordCustomFormatter(x, recordModule) |
| 168 | + } |
| 169 | + else if ((recordVariant = x[Symbol.for('BsVariant')]) !== undefined) { |
| 170 | + return variantCustomFormatter(x,recordVariant) |
| 171 | + } |
| 172 | + else if ((recordPolyVar = x [Symbol.for('BsPolyVar')]) !== undefined){ |
| 173 | + return showObject(x[1]) |
| 174 | + } |
| 175 | + else if(isOCamlExceptionOrExtension(x)){ |
| 176 | + return ["ol", olStyle, ... x.slice(1).map(cur => showObject(cur))] |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | +} |
| 182 | +if (typeof window === "undefined"){ |
| 183 | + global.devtoolsFormatters = [formatter] |
| 184 | +} else { |
| 185 | + window.devtoolsFormatters = [formatter] |
| 186 | +} |
| 187 | +return 0 |
| 188 | + |
| 189 | +|} |
| 190 | + |
| 191 | + |
| 192 | +let setup = ref false |
| 193 | +let setupOnce () = |
| 194 | + if not !setup then |
| 195 | + begin |
| 196 | + setup := true; |
| 197 | + setupChromeDebugger () |
| 198 | + end |
| 199 | + |
| 200 | +type symbol |
| 201 | + |
| 202 | + |
| 203 | +external cacheSymbol : string -> symbol = "for" |
| 204 | + [@@bs.scope "Symbol"] [@@bs.val] |
| 205 | +external addProp : 'a -> symbol -> <value: 'b> Js.t -> 'a = |
| 206 | + "defineProperty" [@@bs.scope "Object"] [@@bs.val] |
| 207 | + |
| 208 | + |
| 209 | +(* It won't affect [Object.keys] using [Object.defineProperty*) |
| 210 | +let record meta xs = |
| 211 | + setupOnce (); |
| 212 | + xs |.addProp (cacheSymbol "BsRecord") [%obj {value = meta}] |
| 213 | + |
| 214 | +let variant meta tag xs = |
| 215 | + setupOnce (); |
| 216 | + xs |. Caml_obj_extern.set_tag tag; |
| 217 | + xs |. addProp (cacheSymbol "BsVariant") [%obj {value = meta }] |
| 218 | + |
| 219 | +let simpleVariant meta xs = |
| 220 | + setupOnce (); |
| 221 | + xs |. addProp (cacheSymbol "BsVariant") [%obj {value = meta }] |
| 222 | + |
| 223 | +let localModule meta xs = |
| 224 | + setupOnce (); |
| 225 | + xs |. addProp (cacheSymbol "BsLocalModule") [%obj {value = meta}] |
| 226 | + |
| 227 | +let polyVar meta xs = |
| 228 | + setupOnce (); |
| 229 | + xs |. addProp (cacheSymbol "BsPolyVar") [%obj {value = meta}] |
0 commit comments