Guest User

Untitled

a guest
Dec 29th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.65 KB | None | 0 0
  1. # HG changeset patch
  2. # User hongzhidao <hongzhidao@gmail.com>
  3. # Date 1546019024 -28800
  4. # Sat Dec 29 01:43:44 2018 +0800
  5. # Node ID 792dbba9b9022ef6435f6f07a0b83cf5a723c8db
  6. # Parent c4ec36309dda20b888ba65dc9ffa3f6de8945c8f
  7. Improved njs_generate_typeof_operation().
  8.  
  9. njs_variable_typeof() cannot fail.
  10.  
  11. diff --git a/njs/njs_generator.c b/njs/njs_generator.c
  12. --- a/njs/njs_generator.c
  13. +++ b/njs/njs_generator.c
  14. @@ -2103,19 +2103,13 @@ njs_generate_typeof_operation(njs_vm_t *
  15. njs_parser_node_t *node)
  16. {
  17. nxt_int_t ret;
  18. - njs_index_t index;
  19. njs_parser_node_t *expr;
  20. njs_vmcode_2addr_t *code;
  21.  
  22. expr = node->left;
  23.  
  24. if (expr->token == NJS_TOKEN_NAME) {
  25. - index = njs_variable_typeof(vm, expr);
  26. - if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
  27. - return NXT_ERROR;
  28. - }
  29. -
  30. - expr->index = index;
  31. + expr->index = njs_variable_typeof(vm, expr);
  32.  
  33. } else {
  34. ret = njs_generator(vm, generator, node->left);
  35. # HG changeset patch
  36. # User hongzhidao <hongzhidao@gmail.com>
  37. # Date 1546019024 -28800
  38. # Sat Dec 29 01:43:44 2018 +0800
  39. # Node ID 9fb7bcaf07ba73f8bff9d06c3d47228965745317
  40. # Parent 792dbba9b9022ef6435f6f07a0b83cf5a723c8db
  41. Variables handling during code generation is improved.
  42.  
  43. All the necessary fields related to a variable are moved into
  44. njs_variable_reference_t.
  45.  
  46. diff --git a/njs/njs_parser.c b/njs/njs_parser.c
  47. --- a/njs/njs_parser.c
  48. +++ b/njs/njs_parser.c
  49. @@ -440,7 +440,7 @@ njs_parser_variable_add(njs_vm_t *vm, nj
  50.  
  51. nxt_inline njs_ret_t
  52. njs_parser_variable_reference(njs_vm_t *vm, njs_parser_t *parser,
  53. - njs_parser_node_t *node, njs_variable_reference_t type)
  54. + njs_parser_node_t *node, njs_reference_type_t type)
  55. {
  56. return njs_variable_reference(vm, parser->scope, node, &parser->lexer->text,
  57. parser->lexer->key_hash, type);
  58. diff --git a/njs/njs_parser.h b/njs/njs_parser.h
  59. --- a/njs/njs_parser.h
  60. +++ b/njs/njs_parser.h
  61. @@ -254,14 +254,12 @@ struct njs_parser_scope_s {
  62. struct njs_parser_node_s {
  63. njs_token_t token:16;
  64. uint8_t ctor:1;
  65. - njs_variable_reference_t reference:2;
  66. uint8_t temporary; /* 1 bit */
  67. uint32_t token_line;
  68. - uint32_t variable_name_hash;
  69.  
  70. union {
  71. uint32_t length;
  72. - nxt_str_t variable_name;
  73. + njs_variable_reference_t reference;
  74. njs_value_t value;
  75. njs_vmcode_operation_t operation;
  76. njs_parser_node_t *object;
  77. diff --git a/njs/njs_parser_expression.c b/njs/njs_parser_expression.c
  78. --- a/njs/njs_parser_expression.c
  79. +++ b/njs/njs_parser_expression.c
  80. @@ -670,7 +670,7 @@ njs_parser_unary_expression(njs_vm_t *vm
  81. }
  82.  
  83. if (token == NJS_TOKEN_TYPEOF && node->token == NJS_TOKEN_NAME) {
  84. - node->reference = NJS_TYPEOF;
  85. + node->u.reference.type = NJS_TYPEOF;
  86. }
  87.  
  88. node = njs_parser_node_alloc(vm);
  89. diff --git a/njs/njs_variable.c b/njs/njs_variable.c
  90. --- a/njs/njs_variable.c
  91. +++ b/njs/njs_variable.c
  92. @@ -9,15 +9,8 @@
  93. #include <string.h>
  94.  
  95.  
  96. -typedef struct {
  97. - nxt_lvlhsh_query_t lhq;
  98. - njs_variable_t *variable;
  99. - njs_parser_scope_t *scope;
  100. -} njs_variable_scope_t;
  101. -
  102. -
  103. static njs_ret_t njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope,
  104. - njs_variable_scope_t *vs, nxt_str_t *name, uint32_t hash);
  105. + njs_variable_reference_t *vr);
  106. static njs_variable_t *njs_variable_alloc(njs_vm_t *vm, nxt_str_t *name,
  107. njs_variable_type_t type);
  108.  
  109. @@ -106,7 +99,7 @@ njs_reference_hash_test(nxt_lvlhsh_query
  110.  
  111. node = data;
  112.  
  113. - if (nxt_strstr_eq(&lhq->key, &node->u.variable_name)) {
  114. + if (nxt_strstr_eq(&lhq->key, &node->u.reference.name)) {
  115. return NXT_OK;
  116. }
  117.  
  118. @@ -128,20 +121,23 @@ const nxt_lvlhsh_proto_t njs_reference_
  119. njs_ret_t
  120. njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope,
  121. njs_parser_node_t *node, nxt_str_t *name, uint32_t hash,
  122. - njs_variable_reference_t reference)
  123. + njs_reference_type_t type)
  124. {
  125. - njs_ret_t ret;
  126. - nxt_lvlhsh_query_t lhq;
  127. + njs_ret_t ret;
  128. + nxt_lvlhsh_query_t lhq;
  129. + njs_variable_reference_t *vr;
  130.  
  131. - ret = njs_name_copy(vm, &node->u.variable_name, name);
  132. + vr = &node->u.reference;
  133. +
  134. + ret = njs_name_copy(vm, &vr->name, name);
  135.  
  136. if (nxt_fast_path(ret == NXT_OK)) {
  137. - node->variable_name_hash = hash;
  138. node->scope = scope;
  139. - node->reference = reference;
  140. + vr->hash = hash;
  141. + vr->type = type;
  142.  
  143. - lhq.key_hash = node->variable_name_hash;
  144. - lhq.key = node->u.variable_name;
  145. + lhq.key_hash = hash;
  146. + lhq.key = vr->name;
  147. lhq.proto = &njs_reference_hash_proto;
  148. lhq.replace = 0;
  149. lhq.value = node;
  150. @@ -162,13 +158,13 @@ static njs_ret_t
  151. njs_variables_scope_resolve(njs_vm_t *vm, njs_parser_scope_t *scope,
  152. nxt_bool_t closure)
  153. {
  154. - njs_ret_t ret;
  155. - nxt_queue_t *nested;
  156. - njs_variable_t *var;
  157. - nxt_queue_link_t *lnk;
  158. - njs_parser_node_t *node;
  159. - nxt_lvlhsh_each_t lhe;
  160. - njs_variable_scope_t vs;
  161. + njs_ret_t ret;
  162. + nxt_queue_t *nested;
  163. + njs_variable_t *var;
  164. + nxt_queue_link_t *lnk;
  165. + njs_parser_node_t *node;
  166. + nxt_lvlhsh_each_t lhe;
  167. + njs_variable_reference_t *vr;
  168.  
  169. nested = &scope->nested;
  170.  
  171. @@ -192,19 +188,19 @@ njs_variables_scope_resolve(njs_vm_t *vm
  172. break;
  173. }
  174.  
  175. + vr = &node->u.reference;
  176. +
  177. if (closure) {
  178. - ret = njs_variable_find(vm, node->scope, &vs,
  179. - &node->u.variable_name,
  180. - node->variable_name_hash);
  181. + ret = njs_variable_find(vm, node->scope, vr);
  182. if (nxt_slow_path(ret != NXT_OK)) {
  183. continue;
  184. }
  185.  
  186. - if (vs.scope->type == NJS_SCOPE_GLOBAL) {
  187. + if (vr->scope->type == NJS_SCOPE_GLOBAL) {
  188. continue;
  189. }
  190.  
  191. - if (node->scope->nesting == vs.scope->nesting) {
  192. + if (node->scope->nesting == vr->scope->nesting) {
  193. /*
  194. * A variable is referenced locally here, but may be
  195. * referenced non-locally in other places, skipping.
  196. @@ -216,7 +212,7 @@ njs_variables_scope_resolve(njs_vm_t *vm
  197. var = njs_variable_get(vm, node);
  198.  
  199. if (nxt_slow_path(var == NULL)) {
  200. - if (node->reference != NJS_TYPEOF) {
  201. + if (vr->type != NJS_TYPEOF) {
  202. return NXT_ERROR;
  203. }
  204. }
  205. @@ -255,18 +251,19 @@ njs_variables_scope_reference(njs_vm_t *
  206. njs_index_t
  207. njs_variable_typeof(njs_vm_t *vm, njs_parser_node_t *node)
  208. {
  209. - nxt_int_t ret;
  210. - njs_variable_scope_t vs;
  211. + nxt_int_t ret;
  212. + njs_variable_reference_t *vr;
  213.  
  214. if (node->index != NJS_INDEX_NONE) {
  215. return node->index;
  216. }
  217.  
  218. - ret = njs_variable_find(vm, node->scope, &vs, &node->u.variable_name,
  219. - node->variable_name_hash);
  220. + vr = &node->u.reference;
  221. +
  222. + ret = njs_variable_find(vm, node->scope, vr);
  223.  
  224. if (nxt_fast_path(ret == NXT_OK)) {
  225. - return vs.variable->index;
  226. + return vr->variable->index;
  227. }
  228.  
  229. return NJS_INDEX_NONE;
  230. @@ -295,16 +292,17 @@ njs_variable_index(njs_vm_t *vm, njs_par
  231. njs_variable_t *
  232. njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node)
  233. {
  234. - nxt_int_t ret;
  235. - nxt_uint_t scope_index;
  236. - nxt_array_t *values;
  237. - njs_index_t index;
  238. - njs_value_t *value;
  239. - njs_variable_t *var;
  240. - njs_variable_scope_t vs;
  241. + nxt_int_t ret;
  242. + nxt_uint_t scope_index;
  243. + nxt_array_t *values;
  244. + njs_index_t index;
  245. + njs_value_t *value;
  246. + njs_variable_t *var;
  247. + njs_variable_reference_t *vr;
  248.  
  249. - ret = njs_variable_find(vm, node->scope, &vs, &node->u.variable_name,
  250. - node->variable_name_hash);
  251. + vr = &node->u.reference;
  252. +
  253. + ret = njs_variable_find(vm, node->scope, vr);
  254.  
  255. if (nxt_slow_path(ret != NXT_OK)) {
  256. goto not_found;
  257. @@ -312,11 +310,11 @@ njs_variable_get(njs_vm_t *vm, njs_parse
  258.  
  259. scope_index = 0;
  260.  
  261. - if (vs.scope->type > NJS_SCOPE_GLOBAL) {
  262. - scope_index = (node->scope->nesting != vs.scope->nesting);
  263. + if (vr->scope->type > NJS_SCOPE_GLOBAL) {
  264. + scope_index = (node->scope->nesting != vr->scope->nesting);
  265. }
  266.  
  267. - var = vs.variable;
  268. + var = vr->variable;
  269. index = var->index;
  270.  
  271. if (index != NJS_INDEX_NONE) {
  272. @@ -327,10 +325,10 @@ njs_variable_get(njs_vm_t *vm, njs_parse
  273. return var;
  274. }
  275.  
  276. - vs.scope->argument_closures++;
  277. + vr->scope->argument_closures++;
  278. index = (index >> NJS_SCOPE_SHIFT) + 1;
  279.  
  280. - if (index > 255 || vs.scope->argument_closures == 0) {
  281. + if (index > 255 || vr->scope->argument_closures == 0) {
  282. njs_internal_error(vm, "too many argument closures");
  283.  
  284. return NULL;
  285. @@ -339,11 +337,11 @@ njs_variable_get(njs_vm_t *vm, njs_parse
  286. var->argument = index;
  287. }
  288.  
  289. - if (node->reference != NJS_DECLARATION && var->type <= NJS_VARIABLE_LET) {
  290. + if (vr->type != NJS_DECLARATION && var->type <= NJS_VARIABLE_LET) {
  291. goto not_found;
  292. }
  293.  
  294. - if (vm->options.accumulative && vs.scope->type == NJS_SCOPE_GLOBAL) {
  295. + if (vm->options.accumulative && vr->scope->type == NJS_SCOPE_GLOBAL) {
  296. /*
  297. * When non-clonable VM runs in accumulative mode all
  298. * global variables should be allocated in absolute scope
  299. @@ -359,7 +357,7 @@ njs_variable_get(njs_vm_t *vm, njs_parse
  300. index = (njs_index_t) value;
  301.  
  302. } else {
  303. - values = vs.scope->values[scope_index];
  304. + values = vr->scope->values[scope_index];
  305.  
  306. if (values == NULL) {
  307. values = nxt_array_create(4, sizeof(njs_value_t),
  308. @@ -368,7 +366,7 @@ njs_variable_get(njs_vm_t *vm, njs_parse
  309. return NULL;
  310. }
  311.  
  312. - vs.scope->values[scope_index] = values;
  313. + vr->scope->values[scope_index] = values;
  314. }
  315.  
  316. value = nxt_array_add(values, &njs_array_mem_proto, vm->mem_cache_pool);
  317. @@ -376,8 +374,8 @@ njs_variable_get(njs_vm_t *vm, njs_parse
  318. return NULL;
  319. }
  320.  
  321. - index = vs.scope->next_index[scope_index];
  322. - vs.scope->next_index[scope_index] += sizeof(njs_value_t);
  323. + index = vr->scope->next_index[scope_index];
  324. + vr->scope->next_index[scope_index] += sizeof(njs_value_t);
  325. }
  326.  
  327. if (njs_is_object(&var->value)) {
  328. @@ -395,7 +393,7 @@ njs_variable_get(njs_vm_t *vm, njs_parse
  329. not_found:
  330.  
  331. njs_parser_ref_error(vm, vm->parser, "\"%.*s\" is not defined",
  332. - (int) vs.lhq.key.length, vs.lhq.key.start);
  333. + (int) vr->name.length, vr->name.start);
  334.  
  335. return NULL;
  336. }
  337. @@ -403,19 +401,20 @@ not_found:
  338.  
  339. static njs_ret_t
  340. njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope,
  341. - njs_variable_scope_t *vs, nxt_str_t *name, uint32_t hash)
  342. + njs_variable_reference_t *vr)
  343. {
  344. + nxt_lvlhsh_query_t lhq;
  345. njs_parser_scope_t *parent, *previous;
  346.  
  347. - vs->lhq.key_hash = hash;
  348. - vs->lhq.key = *name;
  349. - vs->lhq.proto = &njs_variables_hash_proto;
  350. + lhq.key_hash = vr->hash;
  351. + lhq.key = vr->name;
  352. + lhq.proto = &njs_variables_hash_proto;
  353.  
  354. previous = NULL;
  355.  
  356. for ( ;; ) {
  357. - if (nxt_lvlhsh_find(&scope->variables, &vs->lhq) == NXT_OK) {
  358. - vs->variable = vs->lhq.value;
  359. + if (nxt_lvlhsh_find(&scope->variables, &lhq) == NXT_OK) {
  360. + vr->variable = lhq.value;
  361.  
  362. if (scope->type == NJS_SCOPE_SHIM) {
  363. scope = previous;
  364. @@ -430,7 +429,7 @@ njs_variable_find(njs_vm_t *vm, njs_pars
  365. }
  366. }
  367.  
  368. - vs->scope = scope;
  369. + vr->scope = scope;
  370.  
  371. return NXT_OK;
  372. }
  373. @@ -439,7 +438,7 @@ njs_variable_find(njs_vm_t *vm, njs_pars
  374.  
  375. if (parent == NULL) {
  376. /* A global scope. */
  377. - vs->scope = scope;
  378. + vr->scope = scope;
  379.  
  380. return NXT_DECLINED;
  381. }
  382. diff --git a/njs/njs_variable.h b/njs/njs_variable.h
  383. --- a/njs/njs_variable.h
  384. +++ b/njs/njs_variable.h
  385. @@ -18,13 +18,6 @@ typedef enum {
  386. } njs_variable_type_t;
  387.  
  388.  
  389. -typedef enum {
  390. - NJS_DECLARATION = 0,
  391. - NJS_REFERENCE,
  392. - NJS_TYPEOF,
  393. -} njs_variable_reference_t;
  394. -
  395. -
  396. typedef struct {
  397. nxt_str_t name;
  398.  
  399. @@ -42,11 +35,27 @@ typedef struct {
  400. + njs_scope_offset((var)->index) - NJS_INDEX_GLOBAL_OFFSET)
  401.  
  402.  
  403. +typedef enum {
  404. + NJS_DECLARATION = 0,
  405. + NJS_REFERENCE,
  406. + NJS_TYPEOF,
  407. +} njs_reference_type_t;
  408. +
  409. +
  410. +typedef struct {
  411. + njs_reference_type_t type;
  412. + uint32_t hash;
  413. + nxt_str_t name;
  414. + njs_variable_t *variable;
  415. + njs_parser_scope_t *scope;
  416. +} njs_variable_reference_t;
  417. +
  418. +
  419. njs_variable_t *njs_variable_add(njs_vm_t *vm, njs_parser_scope_t *scope,
  420. nxt_str_t *name, uint32_t hash, njs_variable_type_t type);
  421. njs_ret_t njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope,
  422. njs_parser_node_t *node, nxt_str_t *name, uint32_t hash,
  423. - njs_variable_reference_t reference);
  424. + njs_reference_type_t type);
  425. njs_ret_t njs_variables_scope_reference(njs_vm_t *vm,
  426. njs_parser_scope_t *scope);
  427. njs_ret_t njs_name_copy(njs_vm_t *vm, nxt_str_t *dst, nxt_str_t *src);
  428. # HG changeset patch
  429. # User hongzhidao <hongzhidao@gmail.com>
  430. # Date 1546019024 -28800
  431. # Sat Dec 29 01:43:44 2018 +0800
  432. # Node ID 9fc5ed136eb63d021b8a34b981097a85c1e193fc
  433. # Parent 9fb7bcaf07ba73f8bff9d06c3d47228965745317
  434. Removed duplicate njs_variable_reference() declaration.
  435.  
  436. diff --git a/njs/njs_parser.h b/njs/njs_parser.h
  437. --- a/njs/njs_parser.h
  438. +++ b/njs/njs_parser.h
  439. @@ -325,9 +325,6 @@ njs_token_t njs_parser_property_name(njs
  440. njs_token_t njs_parser_property_token(njs_parser_t *parser);
  441. njs_token_t njs_parser_token(njs_parser_t *parser);
  442. nxt_int_t njs_parser_string_create(njs_vm_t *vm, njs_value_t *value);
  443. -njs_ret_t njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope,
  444. - njs_parser_node_t *node, nxt_str_t *name, uint32_t hash,
  445. - njs_variable_reference_t reference);
  446. njs_variable_t *njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node);
  447. njs_index_t njs_variable_typeof(njs_vm_t *vm, njs_parser_node_t *node);
  448. njs_index_t njs_variable_index(njs_vm_t *vm, njs_parser_node_t *node);
Add Comment
Please, Sign In to add comment