|
| 1 | +use crate::{ |
| 2 | + vm::opcode::{Operation, RegisterOperand, IndexOperand}, |
| 3 | + Context, JsResult, |
| 4 | +}; |
| 5 | + |
| 6 | +/// `HasRestrictedGlobalProperty` implements the Opcode Operation for `Opcode::HasRestrictedGlobalProperty` |
| 7 | +/// |
| 8 | +/// Operation: |
| 9 | +/// - Performs [`HasRestrictedGlobalProperty ( N )`][spec] |
| 10 | +/// |
| 11 | +/// [spec]: https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty |
| 12 | +pub(crate) struct HasRestrictedGlobalProperty; |
| 13 | + |
| 14 | +impl HasRestrictedGlobalProperty { |
| 15 | + pub(crate) fn operation( |
| 16 | + (dst, index): (RegisterOperand, IndexOperand), |
| 17 | + context: &mut Context, |
| 18 | + ) -> JsResult<()> { |
| 19 | + let code_block = context.vm.frame().code_block(); |
| 20 | + let name = code_block.constant_string(index.into()); |
| 21 | + let result = context.has_restricted_global_property(&name)?; |
| 22 | + context.vm.set_register(dst.into(), result.into()); |
| 23 | + Ok(()) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl Operation for HasRestrictedGlobalProperty { |
| 28 | + const NAME: &'static str = "HasRestrictedGlobalProperty"; |
| 29 | + const INSTRUCTION: &'static str = "INST - HasRestrictedGlobalProperty"; |
| 30 | + const COST: u8 = 4; |
| 31 | +} |
| 32 | + |
| 33 | +/// `CanDeclareGlobalFunction` implements the Opcode Operation for `Opcode::CanDeclareGlobalFunction` |
| 34 | +/// |
| 35 | +/// Operation: |
| 36 | +/// - Performs [`CanDeclareGlobalFunction ( N )`][spec] |
| 37 | +/// |
| 38 | +/// [spec]: https://tc39.es/ecma262/#sec-candeclareglobalfunction |
| 39 | +pub(crate) struct CanDeclareGlobalFunction; |
| 40 | + |
| 41 | +impl CanDeclareGlobalFunction { |
| 42 | + pub(crate) fn operation( |
| 43 | + (dst, index): (RegisterOperand, IndexOperand), |
| 44 | + context: &mut Context, |
| 45 | + ) -> JsResult<()> { |
| 46 | + let code_block = context.vm.frame().code_block(); |
| 47 | + let name = code_block.constant_string(index.into()); |
| 48 | + let result = context.can_declare_global_function(&name)?; |
| 49 | + context.vm.set_register(dst.into(), result.into()); |
| 50 | + Ok(()) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl Operation for CanDeclareGlobalFunction { |
| 55 | + const NAME: &'static str = "CanDeclareGlobalFunction"; |
| 56 | + const INSTRUCTION: &'static str = "INST - CanDeclareGlobalFunction"; |
| 57 | + const COST: u8 = 4; |
| 58 | +} |
| 59 | + |
| 60 | +/// `CanDeclareGlobalVar` implements the Opcode Operation for `Opcode::CanDeclareGlobalVar` |
| 61 | +/// |
| 62 | +/// Operation: |
| 63 | +/// - Performs [`CanDeclareGlobalVar ( N )`][spec] |
| 64 | +/// |
| 65 | +/// [spec]: https://tc39.es/ecma262/#sec-candeclareglobalvar |
| 66 | +pub(crate) struct CanDeclareGlobalVar; |
| 67 | + |
| 68 | +impl CanDeclareGlobalVar { |
| 69 | + pub(crate) fn operation( |
| 70 | + (dst, index): (RegisterOperand, IndexOperand), |
| 71 | + context: &mut Context, |
| 72 | + ) -> JsResult<()> { |
| 73 | + let code_block = context.vm.frame().code_block(); |
| 74 | + let name = code_block.constant_string(index.into()); |
| 75 | + let result = context.can_declare_global_var(&name)?; |
| 76 | + context.vm.set_register(dst.into(), result.into()); |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl Operation for CanDeclareGlobalVar { |
| 82 | + const NAME: &'static str = "CanDeclareGlobalVar"; |
| 83 | + const INSTRUCTION: &'static str = "INST - CanDeclareGlobalVar"; |
| 84 | + const COST: u8 = 4; |
| 85 | +} |
| 86 | + |
| 87 | +/// `CreateGlobalFunctionBinding` implements the Opcode Operation for `Opcode::CreateGlobalFunctionBinding` |
| 88 | +/// |
| 89 | +/// Operation: |
| 90 | +/// - Performs [`CreateGlobalFunctionBinding ( N, V, D )`][spec] |
| 91 | +/// |
| 92 | +/// [spec]: https://tc39.es/ecma262/#sec-createglobalfunctionbinding |
| 93 | +pub(crate) struct CreateGlobalFunctionBinding; |
| 94 | + |
| 95 | +impl CreateGlobalFunctionBinding { |
| 96 | + pub(crate) fn operation( |
| 97 | + (src, configurable, name_index): (RegisterOperand, IndexOperand, IndexOperand), |
| 98 | + context: &mut Context, |
| 99 | + ) -> JsResult<()> { |
| 100 | + let code_block = context.vm.frame().code_block(); |
| 101 | + let name = code_block.constant_string(name_index.into()); |
| 102 | + let value = context.vm.get_register(src.into()).clone(); |
| 103 | + let configurable = u32::from(configurable) != 0; |
| 104 | + |
| 105 | + // Convert JsValue to JsObject |
| 106 | + let function = value |
| 107 | + .as_object() |
| 108 | + .ok_or_else(|| { |
| 109 | + crate::JsNativeError::typ() |
| 110 | + .with_message("value is not an object") |
| 111 | + })? |
| 112 | + .clone(); |
| 113 | + |
| 114 | + context.create_global_function_binding(name, function, configurable)?; |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl Operation for CreateGlobalFunctionBinding { |
| 120 | + const NAME: &'static str = "CreateGlobalFunctionBinding"; |
| 121 | + const INSTRUCTION: &'static str = "INST - CreateGlobalFunctionBinding"; |
| 122 | + const COST: u8 = 4; |
| 123 | +} |
| 124 | + |
| 125 | +/// `CreateGlobalVarBinding` implements the Opcode Operation for `Opcode::CreateGlobalVarBinding` |
| 126 | +/// |
| 127 | +/// Operation: |
| 128 | +/// - Performs [`CreateGlobalVarBinding ( N, D )`][spec] |
| 129 | +/// |
| 130 | +/// [spec]: https://tc39.es/ecma262/#sec-createglobalvarbinding |
| 131 | +pub(crate) struct CreateGlobalVarBinding; |
| 132 | + |
| 133 | +impl CreateGlobalVarBinding { |
| 134 | + pub(crate) fn operation( |
| 135 | + (configurable, name_index): (IndexOperand, IndexOperand), |
| 136 | + context: &mut Context, |
| 137 | + ) -> JsResult<()> { |
| 138 | + let code_block = context.vm.frame().code_block(); |
| 139 | + let name = code_block.constant_string(name_index.into()); |
| 140 | + let configurable = u32::from(configurable) != 0; |
| 141 | + context.create_global_var_binding(name, configurable)?; |
| 142 | + Ok(()) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl Operation for CreateGlobalVarBinding { |
| 147 | + const NAME: &'static str = "CreateGlobalVarBinding"; |
| 148 | + const INSTRUCTION: &'static str = "INST - CreateGlobalVarBinding"; |
| 149 | + const COST: u8 = 4; |
| 150 | +} |
0 commit comments