1to2.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #!/usr/bin/env node
  2. /*********************************************************************
  3. * NAN - Native Abstractions for Node.js
  4. *
  5. * Copyright (c) 2018 NAN contributors
  6. *
  7. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  8. ********************************************************************/
  9. var commander = require('commander'),
  10. fs = require('fs'),
  11. glob = require('glob'),
  12. groups = [],
  13. total = 0,
  14. warning1 = '/* ERROR: Rewrite using Buffer */\n',
  15. warning2 = '\\/\\* ERROR\\: Rewrite using Buffer \\*\\/\\n',
  16. length,
  17. i;
  18. fs.readFile(__dirname + '/package.json', 'utf8', function (err, data) {
  19. if (err) {
  20. throw err;
  21. }
  22. commander
  23. .version(JSON.parse(data).version)
  24. .usage('[options] <file ...>')
  25. .parse(process.argv);
  26. if (!process.argv.slice(2).length) {
  27. commander.outputHelp();
  28. }
  29. });
  30. /* construct strings representing regular expressions
  31. each expression contains a unique group allowing for identification of the match
  32. the index of this key group, relative to the regular expression in question,
  33. is indicated by the first array member */
  34. /* simple substistutions, key group is the entire match, 0 */
  35. groups.push([0, [
  36. '_NAN_',
  37. 'NODE_SET_METHOD',
  38. 'NODE_SET_PROTOTYPE_METHOD',
  39. 'NanAsciiString',
  40. 'NanEscapeScope',
  41. 'NanReturnValue',
  42. 'NanUcs2String'].join('|')]);
  43. /* substitutions of parameterless macros, key group is 1 */
  44. groups.push([1, ['(', [
  45. 'NanEscapableScope',
  46. 'NanReturnNull',
  47. 'NanReturnUndefined',
  48. 'NanScope'].join('|'), ')\\(\\)'].join('')]);
  49. /* replace TryCatch with NanTryCatch once, gobbling possible namespace, key group 2 */
  50. groups.push([2, '(?:(?:v8\\:\\:)?|(Nan)?)(TryCatch)']);
  51. /* NanNew("string") will likely not fail a ToLocalChecked(), key group 1 */
  52. groups.push([1, ['(NanNew)', '(\\("[^\\"]*"[^\\)]*\\))(?!\\.ToLocalChecked\\(\\))'].join('')]);
  53. /* Removed v8 APIs, warn that the code needs rewriting using node::Buffer, key group 2 */
  54. groups.push([2, ['(', warning2, ')?', '^.*?(', [
  55. 'GetIndexedPropertiesExternalArrayDataLength',
  56. 'GetIndexedPropertiesExternalArrayData',
  57. 'GetIndexedPropertiesExternalArrayDataType',
  58. 'GetIndexedPropertiesPixelData',
  59. 'GetIndexedPropertiesPixelDataLength',
  60. 'HasIndexedPropertiesInExternalArrayData',
  61. 'HasIndexedPropertiesInPixelData',
  62. 'SetIndexedPropertiesToExternalArrayData',
  63. 'SetIndexedPropertiesToPixelData'].join('|'), ')'].join('')]);
  64. /* No need for NanScope in V8-exposed methods, key group 2 */
  65. groups.push([2, ['((', [
  66. 'NAN_METHOD',
  67. 'NAN_GETTER',
  68. 'NAN_SETTER',
  69. 'NAN_PROPERTY_GETTER',
  70. 'NAN_PROPERTY_SETTER',
  71. 'NAN_PROPERTY_ENUMERATOR',
  72. 'NAN_PROPERTY_DELETER',
  73. 'NAN_PROPERTY_QUERY',
  74. 'NAN_INDEX_GETTER',
  75. 'NAN_INDEX_SETTER',
  76. 'NAN_INDEX_ENUMERATOR',
  77. 'NAN_INDEX_DELETER',
  78. 'NAN_INDEX_QUERY'].join('|'), ')\\([^\\)]*\\)\\s*\\{)\\s*NanScope\\(\\)\\s*;'].join('')]);
  79. /* v8::Value::ToXXXXXXX returns v8::MaybeLocal<T>, key group 3 */
  80. groups.push([3, ['([\\s\\(\\)])([^\\s\\(\\)]+)->(', [
  81. 'Boolean',
  82. 'Number',
  83. 'String',
  84. 'Object',
  85. 'Integer',
  86. 'Uint32',
  87. 'Int32'].join('|'), ')\\('].join('')]);
  88. /* v8::Value::XXXXXXXValue returns v8::Maybe<T>, key group 3 */
  89. groups.push([3, ['([\\s\\(\\)])([^\\s\\(\\)]+)->((?:', [
  90. 'Boolean',
  91. 'Number',
  92. 'Integer',
  93. 'Uint32',
  94. 'Int32'].join('|'), ')Value)\\('].join('')]);
  95. /* NAN_WEAK_CALLBACK macro was removed, write out callback definition, key group 1 */
  96. groups.push([1, '(NAN_WEAK_CALLBACK)\\(([^\\s\\)]+)\\)']);
  97. /* node::ObjectWrap and v8::Persistent have been replaced with Nan implementations, key group 1 */
  98. groups.push([1, ['(', [
  99. 'NanDisposePersistent',
  100. 'NanObjectWrapHandle'].join('|'), ')\\s*\\(\\s*([^\\s\\)]+)'].join('')]);
  101. /* Since NanPersistent there is no need for NanMakeWeakPersistent, key group 1 */
  102. groups.push([1, '(NanMakeWeakPersistent)\\s*\\(\\s*([^\\s,]+)\\s*,\\s*']);
  103. /* Many methods of v8::Object and others now return v8::MaybeLocal<T>, key group 3 */
  104. groups.push([3, ['([\\s])([^\\s]+)->(', [
  105. 'GetEndColumn',
  106. 'GetFunction',
  107. 'GetLineNumber',
  108. 'NewInstance',
  109. 'GetPropertyNames',
  110. 'GetOwnPropertyNames',
  111. 'GetSourceLine',
  112. 'GetStartColumn',
  113. 'ObjectProtoToString',
  114. 'ToArrayIndex',
  115. 'ToDetailString',
  116. 'CallAsConstructor',
  117. 'CallAsFunction',
  118. 'CloneElementAt',
  119. 'Delete',
  120. 'ForceSet',
  121. 'Get',
  122. 'GetPropertyAttributes',
  123. 'GetRealNamedProperty',
  124. 'GetRealNamedPropertyInPrototypeChain',
  125. 'Has',
  126. 'HasOwnProperty',
  127. 'HasRealIndexedProperty',
  128. 'HasRealNamedCallbackProperty',
  129. 'HasRealNamedProperty',
  130. 'Set',
  131. 'SetAccessor',
  132. 'SetIndexedPropertyHandler',
  133. 'SetNamedPropertyHandler',
  134. 'SetPrototype'].join('|'), ')\\('].join('')]);
  135. /* You should get an error if any of these fail anyways,
  136. or handle the error better, it is indicated either way, key group 2 */
  137. groups.push([2, ['NanNew(<(?:v8\\:\\:)?(', ['Date', 'String', 'RegExp'].join('|'), ')>)(\\([^\\)]*\\))(?!\\.ToLocalChecked\\(\\))'].join('')]);
  138. /* v8::Value::Equals now returns a v8::Maybe, key group 3 */
  139. groups.push([3, '([\\s\\(\\)])([^\\s\\(\\)]+)->(Equals)\\(([^\\s\\)]+)']);
  140. /* NanPersistent makes this unnecessary, key group 1 */
  141. groups.push([1, '(NanAssignPersistent)(?:<v8\\:\\:[^>]+>)?\\(([^,]+),\\s*']);
  142. /* args has been renamed to info, key group 2 */
  143. groups.push([2, '(\\W)(args)(\\W)'])
  144. /* node::ObjectWrap was replaced with NanObjectWrap, key group 2 */
  145. groups.push([2, '(\\W)(?:node\\:\\:)?(ObjectWrap)(\\W)']);
  146. /* v8::Persistent was replaced with NanPersistent, key group 2 */
  147. groups.push([2, '(\\W)(?:v8\\:\\:)?(Persistent)(\\W)']);
  148. /* counts the number of capturing groups in a well-formed regular expression,
  149. ignoring non-capturing groups and escaped parentheses */
  150. function groupcount(s) {
  151. var positive = s.match(/\((?!\?)/g),
  152. negative = s.match(/\\\(/g);
  153. return (positive ? positive.length : 0) - (negative ? negative.length : 0);
  154. }
  155. /* compute the absolute position of each key group in the joined master RegExp */
  156. for (i = 1, length = groups.length; i < length; i++) {
  157. total += groupcount(groups[i - 1][1]);
  158. groups[i][0] += total;
  159. }
  160. /* create the master RegExp, whis is the union of all the groups' expressions */
  161. master = new RegExp(groups.map(function (a) { return a[1]; }).join('|'), 'gm');
  162. /* replacement function for String.replace, receives 21 arguments */
  163. function replace() {
  164. /* simple expressions */
  165. switch (arguments[groups[0][0]]) {
  166. case '_NAN_':
  167. return 'NAN_';
  168. case 'NODE_SET_METHOD':
  169. return 'NanSetMethod';
  170. case 'NODE_SET_PROTOTYPE_METHOD':
  171. return 'NanSetPrototypeMethod';
  172. case 'NanAsciiString':
  173. return 'NanUtf8String';
  174. case 'NanEscapeScope':
  175. return 'scope.Escape';
  176. case 'NanReturnNull':
  177. return 'info.GetReturnValue().SetNull';
  178. case 'NanReturnValue':
  179. return 'info.GetReturnValue().Set';
  180. case 'NanUcs2String':
  181. return 'v8::String::Value';
  182. default:
  183. }
  184. /* macros without arguments */
  185. switch (arguments[groups[1][0]]) {
  186. case 'NanEscapableScope':
  187. return 'NanEscapableScope scope'
  188. case 'NanReturnUndefined':
  189. return 'return';
  190. case 'NanScope':
  191. return 'NanScope scope';
  192. default:
  193. }
  194. /* TryCatch, emulate negative backref */
  195. if (arguments[groups[2][0]] === 'TryCatch') {
  196. return arguments[groups[2][0] - 1] ? arguments[0] : 'NanTryCatch';
  197. }
  198. /* NanNew("foo") --> NanNew("foo").ToLocalChecked() */
  199. if (arguments[groups[3][0]] === 'NanNew') {
  200. return [arguments[0], '.ToLocalChecked()'].join('');
  201. }
  202. /* insert warning for removed functions as comment on new line above */
  203. switch (arguments[groups[4][0]]) {
  204. case 'GetIndexedPropertiesExternalArrayData':
  205. case 'GetIndexedPropertiesExternalArrayDataLength':
  206. case 'GetIndexedPropertiesExternalArrayDataType':
  207. case 'GetIndexedPropertiesPixelData':
  208. case 'GetIndexedPropertiesPixelDataLength':
  209. case 'HasIndexedPropertiesInExternalArrayData':
  210. case 'HasIndexedPropertiesInPixelData':
  211. case 'SetIndexedPropertiesToExternalArrayData':
  212. case 'SetIndexedPropertiesToPixelData':
  213. return arguments[groups[4][0] - 1] ? arguments[0] : [warning1, arguments[0]].join('');
  214. default:
  215. }
  216. /* remove unnecessary NanScope() */
  217. switch (arguments[groups[5][0]]) {
  218. case 'NAN_GETTER':
  219. case 'NAN_METHOD':
  220. case 'NAN_SETTER':
  221. case 'NAN_INDEX_DELETER':
  222. case 'NAN_INDEX_ENUMERATOR':
  223. case 'NAN_INDEX_GETTER':
  224. case 'NAN_INDEX_QUERY':
  225. case 'NAN_INDEX_SETTER':
  226. case 'NAN_PROPERTY_DELETER':
  227. case 'NAN_PROPERTY_ENUMERATOR':
  228. case 'NAN_PROPERTY_GETTER':
  229. case 'NAN_PROPERTY_QUERY':
  230. case 'NAN_PROPERTY_SETTER':
  231. return arguments[groups[5][0] - 1];
  232. default:
  233. }
  234. /* Value conversion */
  235. switch (arguments[groups[6][0]]) {
  236. case 'Boolean':
  237. case 'Int32':
  238. case 'Integer':
  239. case 'Number':
  240. case 'Object':
  241. case 'String':
  242. case 'Uint32':
  243. return [arguments[groups[6][0] - 2], 'NanTo<v8::', arguments[groups[6][0]], '>(', arguments[groups[6][0] - 1]].join('');
  244. default:
  245. }
  246. /* other value conversion */
  247. switch (arguments[groups[7][0]]) {
  248. case 'BooleanValue':
  249. return [arguments[groups[7][0] - 2], 'NanTo<bool>(', arguments[groups[7][0] - 1]].join('');
  250. case 'Int32Value':
  251. return [arguments[groups[7][0] - 2], 'NanTo<int32_t>(', arguments[groups[7][0] - 1]].join('');
  252. case 'IntegerValue':
  253. return [arguments[groups[7][0] - 2], 'NanTo<int64_t>(', arguments[groups[7][0] - 1]].join('');
  254. case 'Uint32Value':
  255. return [arguments[groups[7][0] - 2], 'NanTo<uint32_t>(', arguments[groups[7][0] - 1]].join('');
  256. default:
  257. }
  258. /* NAN_WEAK_CALLBACK */
  259. if (arguments[groups[8][0]] === 'NAN_WEAK_CALLBACK') {
  260. return ['template<typename T>\nvoid ',
  261. arguments[groups[8][0] + 1], '(const NanWeakCallbackInfo<T> &data)'].join('');
  262. }
  263. /* use methods on NAN classes instead */
  264. switch (arguments[groups[9][0]]) {
  265. case 'NanDisposePersistent':
  266. return [arguments[groups[9][0] + 1], '.Reset('].join('');
  267. case 'NanObjectWrapHandle':
  268. return [arguments[groups[9][0] + 1], '->handle('].join('');
  269. default:
  270. }
  271. /* use method on NanPersistent instead */
  272. if (arguments[groups[10][0]] === 'NanMakeWeakPersistent') {
  273. return arguments[groups[10][0] + 1] + '.SetWeak(';
  274. }
  275. /* These return Maybes, the upper ones take no arguments */
  276. switch (arguments[groups[11][0]]) {
  277. case 'GetEndColumn':
  278. case 'GetFunction':
  279. case 'GetLineNumber':
  280. case 'GetOwnPropertyNames':
  281. case 'GetPropertyNames':
  282. case 'GetSourceLine':
  283. case 'GetStartColumn':
  284. case 'NewInstance':
  285. case 'ObjectProtoToString':
  286. case 'ToArrayIndex':
  287. case 'ToDetailString':
  288. return [arguments[groups[11][0] - 2], 'Nan', arguments[groups[11][0]], '(', arguments[groups[11][0] - 1]].join('');
  289. case 'CallAsConstructor':
  290. case 'CallAsFunction':
  291. case 'CloneElementAt':
  292. case 'Delete':
  293. case 'ForceSet':
  294. case 'Get':
  295. case 'GetPropertyAttributes':
  296. case 'GetRealNamedProperty':
  297. case 'GetRealNamedPropertyInPrototypeChain':
  298. case 'Has':
  299. case 'HasOwnProperty':
  300. case 'HasRealIndexedProperty':
  301. case 'HasRealNamedCallbackProperty':
  302. case 'HasRealNamedProperty':
  303. case 'Set':
  304. case 'SetAccessor':
  305. case 'SetIndexedPropertyHandler':
  306. case 'SetNamedPropertyHandler':
  307. case 'SetPrototype':
  308. return [arguments[groups[11][0] - 2], 'Nan', arguments[groups[11][0]], '(', arguments[groups[11][0] - 1], ', '].join('');
  309. default:
  310. }
  311. /* Automatic ToLocalChecked(), take it or leave it */
  312. switch (arguments[groups[12][0]]) {
  313. case 'Date':
  314. case 'String':
  315. case 'RegExp':
  316. return ['NanNew', arguments[groups[12][0] - 1], arguments[groups[12][0] + 1], '.ToLocalChecked()'].join('');
  317. default:
  318. }
  319. /* NanEquals is now required for uniformity */
  320. if (arguments[groups[13][0]] === 'Equals') {
  321. return [arguments[groups[13][0] - 1], 'NanEquals(', arguments[groups[13][0] - 1], ', ', arguments[groups[13][0] + 1]].join('');
  322. }
  323. /* use method on replacement class instead */
  324. if (arguments[groups[14][0]] === 'NanAssignPersistent') {
  325. return [arguments[groups[14][0] + 1], '.Reset('].join('');
  326. }
  327. /* args --> info */
  328. if (arguments[groups[15][0]] === 'args') {
  329. return [arguments[groups[15][0] - 1], 'info', arguments[groups[15][0] + 1]].join('');
  330. }
  331. /* ObjectWrap --> NanObjectWrap */
  332. if (arguments[groups[16][0]] === 'ObjectWrap') {
  333. return [arguments[groups[16][0] - 1], 'NanObjectWrap', arguments[groups[16][0] + 1]].join('');
  334. }
  335. /* Persistent --> NanPersistent */
  336. if (arguments[groups[17][0]] === 'Persistent') {
  337. return [arguments[groups[17][0] - 1], 'NanPersistent', arguments[groups[17][0] + 1]].join('');
  338. }
  339. /* This should not happen. A switch is probably missing a case if it does. */
  340. throw 'Unhandled match: ' + arguments[0];
  341. }
  342. /* reads a file, runs replacement and writes it back */
  343. function processFile(file) {
  344. fs.readFile(file, {encoding: 'utf8'}, function (err, data) {
  345. if (err) {
  346. throw err;
  347. }
  348. /* run replacement twice, might need more runs */
  349. fs.writeFile(file, data.replace(master, replace).replace(master, replace), function (err) {
  350. if (err) {
  351. throw err;
  352. }
  353. });
  354. });
  355. }
  356. /* process file names from command line and process the identified files */
  357. for (i = 2, length = process.argv.length; i < length; i++) {
  358. glob(process.argv[i], function (err, matches) {
  359. if (err) {
  360. throw err;
  361. }
  362. matches.forEach(processFile);
  363. });
  364. }