Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.06 KB | None | 0 0
  1. let currentTypes = null;
  2.  
  3. function wrapWithTypes (types, fn) {
  4. return function (...args) {
  5. const oldTypes = currentTypes;
  6. currentTypes = types;
  7. try {
  8. return fn.apply(this, args);
  9. } finally {
  10. currentTypes = oldTypes;
  11. }
  12. };
  13. }
  14.  
  15. function wrapDuktapeThread (argument) {
  16. return [
  17. {
  18. "type": "VariableDeclaration",
  19. "declarations": [
  20. {
  21. "type": "VariableDeclarator",
  22. "id": {
  23. "type": "Identifier",
  24. "name": "__thread"
  25. },
  26. "init": {
  27. "type": "NewExpression",
  28. "callee": {
  29. "type": "MemberExpression",
  30. "object": {
  31. "type": "Identifier",
  32. "name": "Duktape"
  33. },
  34. "property": {
  35. "type": "Identifier",
  36. "name": "Thread"
  37. },
  38. "computed": false
  39. },
  40. "arguments": [
  41. argument
  42. ]
  43. }
  44. }
  45. ],
  46. "kind": "var"
  47. },
  48. {
  49. "type": "VariableDeclaration",
  50. "declarations": [
  51. {
  52. "type": "VariableDeclarator",
  53. "id": {
  54. "type": "Identifier",
  55. "name": "__iterDone"
  56. },
  57. "init": null
  58. }
  59. ],
  60. "kind": "var"
  61. },
  62. {
  63. "type": "ReturnStatement",
  64. "argument": {
  65. "type": "ObjectExpression",
  66. "properties": [
  67. {
  68. "type": "ObjectMethod",
  69. "method": true,
  70. "shorthand": false,
  71. "computed": false,
  72. "key": {
  73. "type": "Identifier",
  74. "name": "next"
  75. },
  76. "kind": "method",
  77. "id": null,
  78. "generator": false,
  79. "expression": false,
  80. "async": false,
  81. "params": [],
  82. "body": {
  83. "type": "BlockStatement",
  84. "body": [
  85. {
  86. "type": "IfStatement",
  87. "test": {
  88. "type": "UnaryExpression",
  89. "operator": "!",
  90. "prefix": true,
  91. "argument": {
  92. "type": "Identifier",
  93. "name": "__iterDone"
  94. },
  95. "extra": {
  96. "parenthesizedArgument": false
  97. }
  98. },
  99. "consequent": {
  100. "type": "BlockStatement",
  101. "body": [
  102. {
  103. "type": "VariableDeclaration",
  104. "declarations": [
  105. {
  106. "type": "VariableDeclarator",
  107. "id": {
  108. "type": "Identifier",
  109. "name": "ret"
  110. },
  111. "init": {
  112. "type": "CallExpression",
  113. "callee": {
  114. "type": "MemberExpression",
  115. "object": {
  116. "type": "MemberExpression",
  117. "object": {
  118. "type": "Identifier",
  119. "name": "Duktape"
  120. },
  121. "property": {
  122. "type": "Identifier",
  123. "name": "Thread"
  124. },
  125. "computed": false
  126. },
  127. "property": {
  128. "type": "Identifier",
  129. "name": "resume"
  130. },
  131. "computed": false
  132. },
  133. "arguments": [
  134. {
  135. "type": "Identifier",
  136. "name": "__thread"
  137. }
  138. ]
  139. }
  140. }
  141. ],
  142. "kind": "var"
  143. },
  144. {
  145. "type": "IfStatement",
  146. "test": {
  147. "type": "LogicalExpression",
  148. "left": {
  149. "type": "Identifier",
  150. "name": "ret"
  151. },
  152. "operator": "&&",
  153. "right": {
  154. "type": "MemberExpression",
  155. "object": {
  156. "type": "Identifier",
  157. "name": "ret"
  158. },
  159. "property": {
  160. "type": "Identifier",
  161. "name": "__iter"
  162. },
  163. "computed": false
  164. }
  165. },
  166. "consequent": {
  167. "type": "BlockStatement",
  168. "body": [
  169. {
  170. "type": "ReturnStatement",
  171. "argument": {
  172. "type": "MemberExpression",
  173. "object": {
  174. "type": "Identifier",
  175. "name": "ret"
  176. },
  177. "property": {
  178. "type": "Identifier",
  179. "name": "next"
  180. },
  181. "computed": false
  182. }
  183. }
  184. ],
  185. "directives": []
  186. },
  187. "alternate": null
  188. }
  189. ],
  190. "directives": []
  191. },
  192. "alternate": null
  193. },
  194. {
  195. "type": "ReturnStatement",
  196. "argument": {
  197. "type": "AssignmentExpression",
  198. "operator": "=",
  199. "left": {
  200. "type": "Identifier",
  201. "name": "__iterDone"
  202. },
  203. "right": {
  204. "type": "ObjectExpression",
  205. "properties": [
  206. {
  207. "type": "ObjectProperty",
  208. "method": false,
  209. "shorthand": false,
  210. "computed": false,
  211. "key": {
  212. "type": "Identifier",
  213. "name": "value"
  214. },
  215. "value": {
  216. "type": "Identifier",
  217. "name": "ret"
  218. }
  219. },
  220. {
  221. "type": "ObjectProperty",
  222. "method": false,
  223. "shorthand": false,
  224. "computed": false,
  225. "key": {
  226. "type": "Identifier",
  227. "name": "done"
  228. },
  229. "value": {
  230. "type": "BooleanLiteral",
  231. "value": true
  232. }
  233. }
  234. ]
  235. }
  236. }
  237. }
  238. ],
  239. "directives": []
  240. }
  241. }
  242. ]
  243. }
  244. }
  245. ];
  246. }
  247.  
  248. function wrapYieldStatement (argument) {
  249. return {
  250. "type": "ExpressionStatement",
  251. "expression": {
  252. "type": "CallExpression",
  253. "callee": {
  254. "type": "MemberExpression",
  255. "object": {
  256. "type": "MemberExpression",
  257. "object": {
  258. "type": "Identifier",
  259. "name": "Duktape"
  260. },
  261. "property": {
  262. "type": "Identifier",
  263. "name": "Thread"
  264. },
  265. "computed": false
  266. },
  267. "property": {
  268. "type": "Identifier",
  269. "name": "yield"
  270. },
  271. "computed": false
  272. },
  273. "arguments": [
  274. {
  275. "type": "ObjectExpression",
  276. "properties": [
  277. {
  278. "type": "ObjectProperty",
  279. "method": false,
  280. "shorthand": false,
  281. "computed": false,
  282. "key": {
  283. "type": "Identifier",
  284. "name": "__iter"
  285. },
  286. "value": {
  287. "type": "BooleanLiteral",
  288. "value": true
  289. }
  290. },
  291. {
  292. "type": "ObjectProperty",
  293. "method": false,
  294. "shorthand": false,
  295. "computed": false,
  296. "key": {
  297. "type": "Identifier",
  298. "name": "next"
  299. },
  300. "value": {
  301. "type": "ObjectExpression",
  302. "properties": [
  303. {
  304. "type": "ObjectProperty",
  305. "method": false,
  306. "shorthand": false,
  307. "computed": false,
  308. "key": {
  309. "type": "Identifier",
  310. "name": "done"
  311. },
  312. "value": {
  313. "type": "BooleanLiteral",
  314. "value": false
  315. }
  316. },
  317. {
  318. "type": "ObjectProperty",
  319. "method": false,
  320. "shorthand": false,
  321. "computed": false,
  322. "key": {
  323. "type": "Identifier",
  324. "name": "value"
  325. },
  326. "value": argument
  327. }
  328. ]
  329. }
  330. }
  331. ]
  332. }
  333. ]
  334. }
  335. }
  336. }
  337.  
  338. module.exports = ({ types: t }) => {
  339. return {
  340. visitor: {
  341. YieldExpression (path, state) {
  342. console.log();
  343. const node = t.cloneNode(path.get("argument").node);
  344. // path.get(body).set("body", )
  345. path.replaceWith(wrapYieldStatement(node));
  346. },
  347. Method (path, state) {
  348. let node = path.node;
  349.  
  350. if (!shouldRegenerate(node, state)) return;
  351.  
  352. const container = t.functionExpression(
  353. null,
  354. [],
  355. t.cloneNode(node.body, false),
  356. node.generator,
  357. node.async,
  358. );
  359.  
  360. path.get("body").set("body", [
  361. t.returnStatement(
  362. t.callExpression(container, []),
  363. ),
  364. ]);
  365.  
  366.  
  367. // Regardless of whether or not the wrapped function is a an async method
  368. // or generator the outer function should not be
  369. node.generator = false;
  370.  
  371. // Unwrap the wrapper IIFE's environment so super and this and such still work.
  372. path
  373. .get("body.body.0.argument.callee")
  374. .unwrapFunctionEnvironment();
  375.  
  376. },
  377. Function: {
  378. exit: wrapWithTypes(t, function (path, state) {
  379. console.log();
  380. let node = path.node;
  381.  
  382. if (!shouldRegenerate(node, state)) return;
  383.  
  384.  
  385. const container = t.functionExpression(
  386. null,
  387. [],
  388. t.cloneNode(node.body, false),
  389. false,
  390. node.async,
  391. );
  392.  
  393. node.generator = false;
  394.  
  395. path.get("body").set("body",
  396. wrapDuktapeThread(container)
  397. );
  398.  
  399. path
  400. .get("body.body.0.declarations.0.init.arguments.0")
  401. .unwrapFunctionEnvironment();
  402.  
  403.  
  404. })
  405. }
  406. }
  407. }
  408. }
  409.  
  410.  
  411. // Check if a node should be transformed by regenerator
  412. function shouldRegenerate (node, state) {
  413. return node.generator
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement