@@ -18,10 +18,10 @@ use crate::{
1818 environments:: { BindingLocator , BindingLocatorError , CompileTimeEnvironment } ,
1919 js_string,
2020 vm:: {
21- BindingOpcode , CodeBlock , CodeBlockFlags , GeneratorResumeKind , Handler , Opcode ,
21+ BindingOpcode , CodeBlock , CodeBlockFlags , Constant , GeneratorResumeKind , Handler , Opcode ,
2222 VaryingOperandKind ,
2323 } ,
24- Context , JsBigInt , JsString , JsValue ,
24+ Context , JsBigInt , JsString ,
2525} ;
2626use boa_ast:: {
2727 declaration:: { Binding , LexicalDeclaration , VarDeclaration } ,
@@ -248,21 +248,11 @@ pub struct ByteCompiler<'ctx, 'host> {
248248 /// Bytecode
249249 pub ( crate ) bytecode : Vec < u8 > ,
250250
251- /// Literals
252- pub ( crate ) literals : Vec < JsValue > ,
253-
254- /// Property field names and private name `[[Description]]`s.
255- pub ( crate ) names : Vec < JsString > ,
251+ pub ( crate ) constants : ThinVec < Constant > ,
256252
257253 /// Locators for all bindings in the codeblock.
258254 pub ( crate ) bindings : Vec < BindingLocator > ,
259255
260- /// Functions inside this function
261- pub ( crate ) functions : Vec < Gc < CodeBlock > > ,
262-
263- /// Compile time environments in this function.
264- pub ( crate ) compile_environments : Vec < Rc < CompileTimeEnvironment > > ,
265-
266256 /// The current variable environment.
267257 pub ( crate ) variable_environment : Rc < CompileTimeEnvironment > ,
268258
@@ -315,13 +305,10 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
315305 function_name : name,
316306 length : 0 ,
317307 bytecode : Vec :: default ( ) ,
318- literals : Vec :: default ( ) ,
319- names : Vec :: default ( ) ,
308+ constants : ThinVec :: default ( ) ,
320309 bindings : Vec :: default ( ) ,
321- functions : Vec :: default ( ) ,
322310 this_mode : ThisMode :: Global ,
323311 params : FormalParameterList :: default ( ) ,
324- compile_environments : Vec :: default ( ) ,
325312 current_open_environments_count : 0 ,
326313
327314 // This starts at two because the first value is the `this` value, then function object.
@@ -372,12 +359,12 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
372359 }
373360
374361 let value = match literal. clone ( ) {
375- Literal :: String ( value) => JsValue :: new ( value) ,
376- Literal :: BigInt ( value) => JsValue :: new ( value) ,
362+ Literal :: String ( value) => Constant :: String ( value) ,
363+ Literal :: BigInt ( value) => Constant :: BigInt ( value) ,
377364 } ;
378365
379- let index = self . literals . len ( ) as u32 ;
380- self . literals . push ( value) ;
366+ let index = self . constants . len ( ) as u32 ;
367+ self . constants . push ( value) ;
381368 self . literals_map . insert ( literal, index) ;
382369 index
383370 }
@@ -388,8 +375,8 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
388375 }
389376
390377 let string = self . interner ( ) . resolve_expect ( name. sym ( ) ) . utf16 ( ) ;
391- let index = self . names . len ( ) as u32 ;
392- self . names . push ( js_string ! ( string) ) ;
378+ let index = self . constants . len ( ) as u32 ;
379+ self . constants . push ( Constant :: String ( js_string ! ( string) ) ) ;
393380 self . names_map . insert ( name, index) ;
394381 index
395382 }
@@ -411,6 +398,14 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
411398 index
412399 }
413400
401+ #[ inline]
402+ #[ must_use]
403+ pub ( crate ) fn push_function_to_constants ( & mut self , function : Gc < CodeBlock > ) -> u32 {
404+ let index = self . constants . len ( ) as u32 ;
405+ self . constants . push ( Constant :: Function ( function) ) ;
406+ index
407+ }
408+
414409 fn emit_binding ( & mut self , opcode : BindingOpcode , name : Identifier ) {
415410 match opcode {
416411 BindingOpcode :: Var => {
@@ -1250,10 +1245,7 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
12501245 self . context ,
12511246 ) ;
12521247
1253- let index = self . functions . len ( ) as u32 ;
1254- self . functions . push ( code) ;
1255-
1256- index
1248+ self . push_function_to_constants ( code)
12571249 }
12581250
12591251 /// Compiles a function AST Node into bytecode, setting its corresponding binding or
@@ -1348,8 +1340,7 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
13481340 self . context ,
13491341 ) ;
13501342
1351- let index = self . functions . len ( ) as u32 ;
1352- self . functions . push ( code) ;
1343+ let index = self . push_function_to_constants ( code) ;
13531344
13541345 if r#async && generator {
13551346 self . emit_with_varying_operand ( Opcode :: GetGeneratorAsync , index) ;
@@ -1412,8 +1403,7 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
14121403 self . context ,
14131404 ) ;
14141405
1415- let index = self . functions . len ( ) as u32 ;
1416- self . functions . push ( code) ;
1406+ let index = self . push_function_to_constants ( code) ;
14171407
14181408 if r#async && generator {
14191409 self . emit_with_varying_operand ( Opcode :: GetGeneratorAsync , index) ;
@@ -1535,11 +1525,8 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
15351525 this_mode : self . this_mode ,
15361526 params : self . params ,
15371527 bytecode : self . bytecode . into_boxed_slice ( ) ,
1538- literals : self . literals . into_boxed_slice ( ) ,
1539- names : self . names . into_boxed_slice ( ) ,
1528+ constants : self . constants ,
15401529 bindings : self . bindings . into_boxed_slice ( ) ,
1541- functions : self . functions . into_boxed_slice ( ) ,
1542- compile_environments : self . compile_environments . into_boxed_slice ( ) ,
15431530 handlers : self . handlers ,
15441531 flags : Cell :: new ( self . code_block_flags ) ,
15451532 }
0 commit comments