Best JavaScript code snippet using wpt
debuggerMode.ts
Source:debuggerMode.ts
1import { Component } from '@angular/core';2import { FormsModule, FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';3import { Register } from './Register';4@Component({5 selector: 'my-debugger',6 templateUrl: './debuggerMode.html',7 styleUrls: ['./app.component.scss'],8})9export class DebuggerMode {10 private remainderToDisplay: string = '';11 private arrow: string = '';12 private outputValue: string = '0';13 private valueToAdd: string;14 private valueToAdd2: string = '';15 private textAreaValue: string = '';16 private textAreaValue1: string = '';17 private selectedAssemblyCode: string = '';18 private AssemblyCode: string[] = ['ADD Program', 'SUB Program'];19 private showAdd: boolean = false;20 private showSub: boolean = false;21 private showMul: boolean = false;22 private showDiv: boolean = false;23 private showMov: boolean = false;24 private showInc: boolean = false;25 private showDec: boolean = false;26 private showAnd: boolean = false;27 private showOR: boolean = false;28 private showXOR: boolean = false;29 private showNOT: boolean = false;30 private showPush: boolean = false;31 private showPop: boolean = false;32 private addNumberBoolean: boolean = false;33 private addRegisterBoolean: boolean = false;34 private subNumberBoolean: boolean = false;35 private subRegisterBoolean: boolean = false;36 private mulNumberBoolean: boolean = false;37 private mulRegisterBoolean: boolean = false;38 private mulRegisterTwoBoolean: boolean = false;39 private selectedRegister1: string = '';40 private selectedRegister2: string = '';41 private selectedRegister3: string = '';42 private selectedRegister4: string = '';43 private selectedRegister5: string = '';44 private register: Register[] = [45 new Register('RAX', '0000000000000005', ''),46 new Register('RBX', '0000000000000006', ''),47 new Register('RCX', '0000000000000015', ''),48 new Register('RDX', '0000000000000012', ''),49 new Register('RBP', '00000000000000a1', ''),50 new Register('RSP', '0000000000FEF1B0', ''),51 new Register('RSI', '000000000000002d', ''),52 new Register('RDI', '000000000000003a', ''),53 new Register('RIP', '000000000000009b', ''),54 new Register('R8', '0000000000000000', ''),55 new Register('R9', '0000000000000000', ''),56 new Register('R10', '0000000000000000', ''),57 new Register('R11', '0000000000000000', ''),58 new Register('R12', '0000000000000000', ''),59 new Register('R13', '0000000000000000', ''),60 new Register('R14', '0000000000000000', ''),61 new Register('R15', '0000000000000000', ''),62 new Register('0000000000FEF150', '0000000000000010', ''),63 new Register('0000000000FEF158', '000000000000000a', ''),64 new Register('0000000000FEF160', '00000000000000c5', ''),65 new Register('0000000000FEF168', '00000000000000a3', ''),66 new Register('0000000000FEF170', '0000000000000180', ''),67 new Register('0000000000FEF178', '0000000000000000', ''),68 new Register('0000000000FEF180', '0000000000000000', ''),69 new Register('0000000000FEF188', '0000000000000000', ''),70 new Register('0000000000FEF190', '0000000000000000', ''),71 new Register('0000000000FEF198', '0000000000000000', ''),72 new Register('0000000000FEF1A0', '0000000000000000', ''),73 new Register('0000000000FEF1A8', '0000000000000000', ''),74 new Register('0000000000FEF1B0', '0000000000000008', ''),75 new Register('0000000000FEF1C0', '0000000000000001', ''),76 new Register('0000000000FEF1C8', '0000000000000002', ''),77 new Register('0000000000FEF1D0', '0000000000000003', ''),78 new Register('0000000000FEF1D8', '0000000000000004', '')79 ];80 private memoryList: string[] = ['0000000000FEF150', '0000.000000FEF158', '0000000000FEF1A0', '0000000000FEF1A8', '0000000000FEF1B0'81 , '0000000000FEF1C0', '0000000000FEF1C8', '0000000000FEF1D0', '0000000000FEF1D8'];82 public checkIfRegister(r1: Register) {83 if (r1.name == 'RAX' || r1.name == 'RBX' || r1.name == 'RCX' || r1.name == 'RDX' || r1.name == 'RBP' || r1.name == 'RSP' || r1.name == 'RDI' ||84 r1.name == 'RSI' || r1.name == 'RIP' || r1.name == 'R8' || r1.name == 'R9' || r1.name == 'R10' || r1.name == 'R11' ||85 r1.name == 'R12' || r1.name == 'R13' || r1.name == 'R14' || r1.name == 'R15') {86 return true;87 } else {88 return false;89 }90 }91 public checkIfMemory(r1: Register) {92 if (r1.name == '0000000000FEF150' || r1.name == '0000000000FEF158' || r1.name == '0000000000FEF160' ||93 r1.name == '0000000000FEF168' || r1.name == '0000000000FEF170' || r1.name == '0000000000FEF178' ||94 r1.name == '0000000000FEF180' || r1.name == '0000000000FEF188' || r1.name == '0000000000FEF190' ||95 r1.name == '0000000000FEF198' || r1.name == '0000000000FEF1A0' || r1.name == '0000000000FEF1A8' ||96 r1.name == '0000000000FEF1B0' || r1.name == '0000000000FEF1B8' || r1.name == '0000000000FEF1C0' ||97 r1.name == '0000000000FEF1C8' || r1.name == '0000000000FEF1D0' || r1.name == '0000000000FEF1D8') {98 return true;99 } else {100 return false;101 }102 }103 isHex(h: any) {104 var a = parseInt(h, 16);105 return (a.toString(16) === h.toLowerCase())106 }107 showAssemblyCode() {108 if (this.selectedAssemblyCode == 'ADD Program') {109 this.textAreaValue = 'ADD RAX,RBX,\nADD C,D';110 } else if (this.selectedAssemblyCode == 'SUB Program') {111 this.textAreaValue = 'SUB RBX,RAX';112 }113 }114 executeInstructions() {115 // if(this.validateInstruction()){116 // }117 console.log(this.textAreaValue);118 var lines = this.textAreaValue.split('\n');119 for (var i = 0; i < lines.length; i++) {120 if (lines[i].search('ADD') != -1) {121 let inst: string;122 inst = lines[i].split(' ')[1];123 var instArray = inst.split(',');124 console.log(instArray);125 let tempRegister1: Register = { name: instArray[0], value: '0', arrow: '' };126 if (this.checkIfRegister(tempRegister1) || this.checkIfMemory(tempRegister1)) {127 this.selectedRegister1 = instArray[0];128 console.log(this.selectedRegister1);129 let tempRegister2: Register = { name: instArray[1], value: '0', arrow: '' };130 if (this.checkIfRegister(tempRegister2) || this.checkIfMemory(tempRegister2)) {131 this.selectedRegister3 = instArray[1];132 this.selectedRegister2 = this.selectedRegister1;133 this.selectedRegister1 = '';134 console.log(this.selectedRegister3);135 this.addRegister();136 }137 else {138 if (this.isHex(instArray[1])) {139 this.valueToAdd = instArray[1];140 this.addNumber();141 } else {142 console.log('Error on line ' + i);143 break;144 }145 }146 }147 else {148 console.log('Invalid instruction on line ' + i);149 break;150 }151 }152 if (lines[i].search('SUB') != -1) {153 let inst: string;154 inst = lines[i].split(' ')[1];155 var instArray = inst.split(',');156 console.log(instArray);157 let tempRegister1: Register = { name: instArray[0], value: '0', arrow: '' };158 if (this.checkIfRegister(tempRegister1) || this.checkIfMemory(tempRegister1)) {159 this.selectedRegister1 = instArray[0];160 console.log(this.selectedRegister1);161 let tempRegister2: Register = { name: instArray[1], value: '0', arrow: '' };162 if (this.checkIfRegister(tempRegister2) || this.checkIfMemory(tempRegister2)) {163 this.selectedRegister3 = instArray[1];164 this.selectedRegister2 = this.selectedRegister1;165 this.selectedRegister1 = '';166 console.log(this.selectedRegister3);167 this.subRegister();168 }169 else {170 if (this.isHex(instArray[1])) {171 this.valueToAdd = instArray[1];172 this.subNumber();173 } else {174 console.log('Error on line ' + i);175 break;176 }177 }178 }179 else {180 console.log('Invalid instruction on line ' + i);181 break;182 }183 }184 if (lines[i].search('MUL') != -1) {185 let inst: string;186 inst = lines[i].split(' ')[1];187 var instArray = inst.split(',');188 console.log(instArray);189 let tempRegister1: Register = { name: instArray[0], value: '0', arrow: '' };190 if (this.checkIfRegister(tempRegister1) || this.checkIfMemory(tempRegister1)) {191 this.selectedRegister1 = instArray[0];192 console.log(this.selectedRegister1);193 let tempRegister2: Register = { name: instArray[1], value: '0', arrow: '' };194 if (this.checkIfRegister(tempRegister2) || this.checkIfMemory(tempRegister2)) {195 this.selectedRegister3 = instArray[1];196 this.selectedRegister2 = this.selectedRegister1;197 this.selectedRegister1 = '';198 console.log(this.selectedRegister3);199 this.mulRegister();200 }201 else {202 if (this.isHex(instArray[1])) {203 this.valueToAdd = instArray[1];204 this.mulNumber();205 } else {206 console.log('Error on line ' + i);207 break;208 }209 }210 }211 else {212 console.log('Invalid instruction on line ' + i);213 break;214 }215 }216 if (lines[i].search('DIV') != -1) {217 let inst: string;218 inst = lines[i].split(' ')[1];219 var instArray = inst.split(',');220 console.log(instArray);221 let tempRegister1: Register = { name: instArray[0], value: '0', arrow: '' };222 if (this.checkIfRegister(tempRegister1) || this.checkIfMemory(tempRegister1)) {223 this.selectedRegister1 = instArray[0];224 console.log(this.selectedRegister1);225 let tempRegister2: Register = { name: instArray[1], value: '0', arrow: '' };226 if (this.checkIfRegister(tempRegister2) || this.checkIfMemory(tempRegister2)) {227 this.selectedRegister3 = instArray[1];228 this.selectedRegister2 = this.selectedRegister1;229 this.selectedRegister1 = '';230 console.log(this.selectedRegister3);231 this.divRegister();232 }233 else {234 if (this.isHex(instArray[1])) {235 this.valueToAdd = instArray[1];236 this.divNumber();237 } else {238 console.log('Error on line ' + i);239 break;240 }241 }242 }243 else {244 console.log('Invalid instruction on line ' + i);245 break;246 }247 }248 if (lines[i].search('INC') != -1) {249 250 let inst: string;251 inst = lines[i].split(' ')[1];252 var instArray = inst.split(' ');253 console.log(instArray);254 let tempRegister1: Register = { name: instArray[0], value: '0', arrow: '' };255 if (this.checkIfRegister(tempRegister1) || this.checkIfMemory(tempRegister1)) {256 this.selectedRegister1 = instArray[0];257 console.log(this.selectedRegister1);258 let tempRegister2: Register = { name: instArray[1], value: '0', arrow: '' };259 if (this.checkIfRegister(tempRegister2) || this.checkIfMemory(tempRegister2)) {260 this.selectedRegister3 = instArray[1];261 this.selectedRegister2 = this.selectedRegister1;262 this.selectedRegister1 = '';263 console.log(this.selectedRegister3);264 this.subRegister();265 }266 else {267 if (this.isHex(instArray[1])) {268 this.valueToAdd = instArray[1];269 this.incNumber();270 } else {271 console.log('Error on line ' + i);272 break;273 }274 }275 }276 else {277 console.log('Invalid instruction on line ' + i);278 break;279 }280 }281 }282 }283 ngOnInit() {284 let num1: string = '100';285 let num2: string = '';286 let num3: number = parseInt(num1, 16);287 console.log(num3);288 let num4: string = '6000';289 console.log((parseInt(num4, 16) / parseInt(num1, 16)).toString(16));290 console.log('************************************');291 //this.pushRegister();292 }293 public resetParameters() {294 this.showAdd = false;295 this.showSub = false;296 this.showMul = false;297 this.showDiv = false298 this.showMov = false;299 this.showInc = false;300 this.showDec = false;301 this.showPush = false;302 this.showPop = false;303 this.showAnd = false;304 this.showOR = false;305 this.showXOR = false;306 this.showNOT = false;307 }308 public addNumber(): void {309 this.addRegisterBoolean = false;310 this.addNumberBoolean = true;311 for (let r of this.register) {312 r.arrow = '';313 if (this.selectedRegister1 == r.name) {314 let temp1 = parseInt(r.value, 16);315 let temp2 = parseInt(this.valueToAdd, 16);316 temp1 = temp1 + temp2;317 //append zeros so that length becomes 16318 let temp3 = temp1.toString(16);319 let l = temp3.length;320 let valueToStore: string = '';321 for (var i = 0; i < (16 - l); i++) {322 valueToStore = valueToStore + '0';323 }324 valueToStore = valueToStore + temp3;325 console.log(valueToStore);326 r.value = valueToStore;327 r.arrow = 'fa fa-hand-o-right fa-4x';328 console.log(r.arrow);329 this.outputValue = r.value;330 }331 }332 }333 public addRegister(): void {334 this.addRegisterBoolean = true;335 this.addNumberBoolean = false;336 let temp1: Register = new Register('', '0', '');337 let temp2: Register = new Register('', '0', '');338 for (let r of this.register) {339 r.arrow = '';340 if (this.selectedRegister2 == r.name) {341 temp1 = r;342 }343 }344 for (let r of this.register) {345 if (this.selectedRegister3 == r.name) {346 temp2 = r;347 }348 }349 let temp3 = parseInt(temp1.value, 16);350 let temp4 = parseInt(temp2.value, 16);351 temp3 = temp3 + temp4;352 let temp5 = temp3.toString(16);353 let l = temp5.length;354 let valueToStore: string = '';355 for (let i = 0; i < (16 - l); i++) {356 valueToStore = valueToStore + '0';357 }358 valueToStore += temp5;359 temp1.value = valueToStore;360 temp1.arrow = 'fa fa-hand-o-right fa-4x';361 this.outputValue = temp1.value;362 }363 public subNumber(): void {364 this.subRegisterBoolean = false;365 this.subNumberBoolean = true;366 for (let r of this.register) {367 r.arrow = '';368 if (this.selectedRegister1 == r.name) {369 let temp1 = parseInt(r.value, 16);370 let temp2 = parseInt(this.valueToAdd, 16);371 temp1 = temp1 - temp2;372 //append zeros so that length becomes 16373 let temp3 = temp1.toString(16);374 let l = temp3.length;375 let valueToStore: string = '';376 for (var i = 0; i < (16 - l); i++) {377 valueToStore = valueToStore + '0';378 }379 valueToStore = valueToStore + temp3;380 console.log(valueToStore);381 r.value = valueToStore;382 r.arrow = 'fa fa-hand-o-right fa-4x';383 this.outputValue = r.value;384 }385 }386 }387 public mulNumber(): void {388 this.mulRegisterBoolean = false;389 this.mulNumberBoolean = true;390 for (let r of this.register) {391 r.arrow = '';392 if (this.selectedRegister1 == r.name) {393 let temp1 = parseInt(r.value, 16);394 let temp2 = parseInt(this.valueToAdd, 16);395 temp1 = temp1 * temp2;396 //append zeros so that length becomes 16397 let temp3 = temp1.toString(16);398 let l = temp3.length;399 let valueToStore: string = '';400 for (var i = 0; i < (16 - l); i++) {401 valueToStore = valueToStore + '0';402 }403 valueToStore = valueToStore + temp3;404 console.log(valueToStore);405 r.value = valueToStore;406 r.arrow = 'fa fa-hand-o-right fa-4x';407 this.outputValue = r.value;408 }409 }410 }411 public subRegister(): void {412 this.subRegisterBoolean = true;413 this.subNumberBoolean = false;414 let temp1: Register = new Register('', '0', '');415 let temp2: Register = new Register('', '0', '');416 for (let r of this.register) {417 r.arrow = '';418 if (this.selectedRegister2 == r.name) {419 temp1 = r;420 }421 }422 for (let r of this.register) {423 if (this.selectedRegister3 == r.name) {424 temp2 = r;425 }426 }427 let temp3 = parseInt(temp1.value, 16);428 let temp4 = parseInt(temp2.value, 16);429 temp3 = temp3 - temp4;430 let temp5 = temp3.toString(16);431 let l = temp5.length;432 let valueToStore: string = '';433 for (let i = 0; i < (16 - l); i++) {434 valueToStore = valueToStore + '0';435 }436 valueToStore += temp5;437 temp1.value = valueToStore;438 temp1.arrow = 'fa fa-hand-o-right fa-4x'439 this.outputValue = temp1.value;440 }441 public mulRegister(): void {442 this.mulRegisterBoolean = true;443 this.mulNumberBoolean = false;444 this.mulRegisterTwoBoolean = false;445 let temp1: Register = new Register('', '0', '');446 let temp2: Register = new Register('', '0', '');447 for (let r of this.register) {448 r.arrow = '';449 if (this.selectedRegister2 == r.name) {450 temp1 = r;451 }452 }453 for (let r of this.register) {454 if (this.selectedRegister3 == r.name) {455 temp2 = r;456 }457 }458 let temp3 = parseInt(temp1.value, 16);459 let temp4 = parseInt(temp2.value, 16);460 temp3 = temp3 * temp4;461 let temp5 = temp3.toString(16);462 let l = temp5.length;463 let valueToStore: string = '';464 for (let i = 0; i < (16 - l); i++) {465 valueToStore = valueToStore + '0';466 }467 valueToStore += temp5;468 temp1.value = valueToStore;469 temp1.arrow = 'fa fa-hand-o-right fa-4x'470 this.outputValue = temp1.value;471 }472 public mulRegister2(): void {473 this.mulRegisterTwoBoolean = true;474 this.mulNumberBoolean = false;475 this.mulRegisterBoolean = false;476 let temp1: Register = new Register('', '0', '');477 let temp2: Register = new Register('', '0', '');478 let temp6 = parseInt(this.valueToAdd2, 16);479 for (let r of this.register) {480 r.arrow = '';481 if (this.selectedRegister4 == r.name) {482 temp1 = r;483 }484 }485 for (let r of this.register) {486 if (this.selectedRegister5 == r.name) {487 temp2 = r;488 }489 }490 let temp3 = parseInt(temp1.value, 16);491 let temp4 = parseInt(temp2.value, 16);492 temp3 = temp3 * temp4 * temp6;493 let temp5 = temp3.toString(16);494 let l = temp5.length;495 let valueToStore: string = '';496 for (let i = 0; i < (16 - l); i++) {497 valueToStore = valueToStore + '0';498 }499 valueToStore += temp5;500 temp1.value = valueToStore;501 temp1.arrow = 'fa fa-hand-o-right fa-4x'502 this.outputValue = temp1.value;503 }504 public divNumber(): void {505 let temp1 = 0;506 let temp2 = 0;507 let quotient: number = 0;508 let remainder = 0;509 let remainderString = '';510 let valueToStore: string = '';511 for (let r of this.register) {512 r.arrow = '';513 if (this.selectedRegister1 == r.name) {514 temp1 = parseInt(r.value, 16);515 temp2 = parseInt(this.valueToAdd, 16);516 quotient = Math.floor(temp1 / temp2);517 remainder = temp1 % temp2;518 console.log(remainderString);519 temp1 = quotient;520 //append zeros so that length becomes 16521 console.log(temp1);522 let temp3 = temp1.toString(16);523 let l = temp3.length;524 for (var i = 0; i < (16 - l); i++) {525 valueToStore = valueToStore + '0';526 }527 for (var i = 0; i < (16 - l); i++) {528 remainderString = remainderString + '0';529 }530 valueToStore = valueToStore + temp3;531 console.log(valueToStore);532 //r.value = valueToStore;533 // r.arrow='-->';534 }535 }536 for (let r of this.register) {537 if (r.name == 'RAX') {538 r.value = valueToStore;539 r.arrow = 'fa fa-hand-o-right fa-4x';540 }541 if (r.name == 'RDX') {542 remainderString = remainderString + remainder.toString(16);543 console.log(remainderString);544 r.value = remainderString;545 r.arrow = 'fa fa-hand-o-right fa-4x';546 }547 }548 }549 public divRegister(): void {550 let temp1: Register = new Register('', '0', '');551 let temp2: Register = new Register('', '0', '');552 let quotient: number = 0553 let remainder: number = 0;554 let remainderString: string = '';555 for (let r of this.register) {556 r.arrow = '';557 if (this.selectedRegister2 == r.name) {558 temp1 = r;559 }560 }561 for (let r of this.register) {562 if (this.selectedRegister3 == r.name) {563 temp2 = r;564 }565 }566 let temp3 = parseInt(temp1.value, 16);567 let temp4 = parseInt(temp2.value, 16);568 quotient = Math.floor(temp3 / temp4);569 remainder = temp3 % temp4;570 //let temp6 = temp3 % temp4;571 temp3 = quotient;572 let temp5 = temp3.toString(16);573 let l = temp5.length;574 let valueToStore: string = '';575 for (let i = 0; i < (16 - l); i++) {576 valueToStore = valueToStore + '0';577 }578 valueToStore += temp5;579 for (var i = 0; i < (16 - l); i++) {580 remainderString = remainderString + '0';581 }582 for (let r of this.register) {583 if (r.name == 'RAX') {584 r.value = valueToStore;585 r.arrow = 'fa fa-hand-o-right fa-4x';586 this.outputValue = r.value;587 }588 }589 for (let r of this.register) {590 if (r.name == 'RDX') {591 remainderString = remainderString + remainder.toString(16);592 r.value = remainderString;593 r.arrow = 'fa fa-hand-o-right fa-4x';594 this.remainderToDisplay = r.value;595 }596 }597 //this.outputValue = temp1.value;598 }599 public movNumber(): void {600 for (let r of this.register) {601 r.arrow = '';602 if (this.selectedRegister1 == r.name) {603 let temp1 = parseInt(r.value, 16);604 let temp2 = parseInt(this.valueToAdd, 16);605 temp1 = temp2;606 //append zeros so that length becomes 16607 let temp3 = temp1.toString(16);608 let l = temp3.length;609 let valueToStore: string = '';610 for (var i = 0; i < (16 - l); i++) {611 valueToStore = valueToStore + '0';612 }613 valueToStore = valueToStore + temp3;614 console.log(valueToStore);615 r.value = valueToStore;616 r.arrow = 'fa fa-hand-o-right fa-4x';617 console.log(r.arrow);618 }619 }620 }621 public movRegister(): void {622 let temp1: Register = new Register('', '0', '');623 let temp2: Register = new Register('', '0', '');624 for (let r of this.register) {625 r.arrow = '';626 if (this.selectedRegister2 == r.name) {627 temp1 = r;628 }629 }630 for (let r of this.register) {631 if (this.selectedRegister3 == r.name) {632 temp2 = r;633 }634 }635 let temp3 = parseInt(temp1.value, 16);636 let temp4 = parseInt(temp2.value, 16);637 temp3 = temp4;638 let temp5 = temp3.toString(16);639 let l = temp5.length;640 let valueToStore: string = '';641 for (let i = 0; i < (16 - l); i++) {642 valueToStore = valueToStore + '0';643 }644 valueToStore += temp5;645 temp1.value = valueToStore;646 temp1.arrow = 'fa fa-hand-o-right fa-4x';647 this.outputValue = temp1.value;648 }649 public incNumber(): void {650 for (let r of this.register) {651 r.arrow = '';652 if (this.selectedRegister1 == r.name) {653 let temp1 = parseInt(r.value, 16);654 // let temp2 = parseInt(this.valueToAdd, 16);655 temp1 = temp1 + 1;656 //append zeros so that length becomes 16657 let temp3 = temp1.toString(16);658 let l = temp3.length;659 let valueToStore: string = '';660 for (var i = 0; i < (16 - l); i++) {661 valueToStore = valueToStore + '0';662 }663 valueToStore = valueToStore + temp3;664 console.log(valueToStore);665 r.value = valueToStore;666 r.arrow = 'fa fa-hand-o-right fa-4x';667 console.log(r.arrow);668 }669 }670 }671 public decNumber(): void {672 for (let r of this.register) {673 r.arrow = '';674 if (this.selectedRegister1 == r.name) {675 let temp1 = parseInt(r.value, 16);676 // let temp2 = parseInt(this.valueToAdd, 16);677 temp1 = temp1 - 1;678 //append zeros so that length becomes 16679 let temp3 = temp1.toString(16);680 let l = temp3.length;681 let valueToStore: string = '';682 for (var i = 0; i < (16 - l); i++) {683 valueToStore = valueToStore + '0';684 }685 valueToStore = valueToStore + temp3;686 console.log(valueToStore);687 r.value = valueToStore;688 r.arrow = 'fa fa-hand-o-right fa-4x';689 console.log(r.arrow);690 }691 }692 }693 public pushRegister() {694 let tempRSPValue: string;695 let temp: string;696 for (let sRegister of this.register) {697 sRegister.arrow = '';698 if (this.selectedRegister1 == sRegister.name) {699 temp = sRegister.value;700 }701 }702 for (let r = 0; r < this.register.length; r++) {703 if (this.register[r].name == 'RSP') {704 tempRSPValue = this.register[r].value;705 console.log(tempRSPValue);706 //console.log(this.memoryList.length);707 }708 }709 for (let r = 0; r < this.register.length; r++) {710 if (this.register[r].name == tempRSPValue) {711 for (let i = this.register.length - 1; i > r; i--) {712 this.register[i].value = this.register[i - 1].value;713 }714 this.register[r].value = temp;715 this.register[r].arrow = '==>';716 }717 }718 }719 public popRegister() {720 let tempRSPRegister: Register = new Register('', '0', '');721 let tempRSPValue: string;722 let temp: string;723 for (let r = 0; r < this.register.length; r++) {724 this.register[r].arrow = '';725 if (this.register[r].name == 'RSP') {726 tempRSPValue = this.register[r].value;727 console.log(tempRSPValue);728 tempRSPRegister = this.register[r];729 tempRSPRegister.arrow = '==>';730 //console.log(this.memoryList.length);731 }732 }733 for (let r = 0; r < this.register.length; r++) {734 if (this.register[r].name == tempRSPValue) {735 tempRSPRegister.value = this.register[r + 1].name;736 for (let s of this.register) {737 if (this.selectedRegister1 == s.name) {738 s.value = this.register[r].value;739 s.arrow = '==>';740 }741 }742 }743 }744 }745 // this.memoryList[j] = this.memoryList[j-1];746 // }747 // -----------------------------------------------------------748 // for(var i=0;i<this.memoryList.length; i++){749 // console.log(this.memoryList.length);750 // if(this.memoryList[i]!=tempRSPValue){751 // continue; 752 // }753 // if(this.memoryList[i] == tempRSPValue){754 // console.log('Inside if loop');755 // this.pushRegister1(i);756 // }757 // }758 // }759 // public pushRegister1(i: number){760 // console.log('inside pushRegister1 method');761 // //let memoryIndexToInsert: number =i;762 // for(let j =this.memoryList.length-1;j>i;j--){763 // this.memoryList[j] = this.memoryList[j-1];764 // }765 // this.memoryList[i] = '5';766 // console.log(this.memoryList[i]);767 // }768 public andNumber(): void {769 for (let r of this.register) {770 r.arrow = '';771 if (this.selectedRegister1 == r.name) {772 let temp1 = parseInt(r.value, 16);773 let temp2 = parseInt(this.valueToAdd, 16);774 temp1 = temp1 & temp2;775 console.log(temp1);776 //append zeros so that length becomes 16777 let temp3 = temp1.toString(16);778 let l = temp3.length;779 let valueToStore: string = '';780 for (var i = 0; i < (16 - l); i++) {781 valueToStore = valueToStore + '0';782 }783 valueToStore = valueToStore + temp3;784 console.log(valueToStore);785 r.value = valueToStore;786 r.arrow = 'fa fa-hand-o-right fa-4x';787 }788 }789 }790 public andRegister(): void {791 let temp1: Register = new Register('', '0', '');792 let temp2: Register = new Register('', '0', '');793 for (let r of this.register) {794 r.arrow = '';795 if (this.selectedRegister2 == r.name) {796 temp1 = r;797 }798 }799 for (let r of this.register) {800 if (this.selectedRegister3 == r.name) {801 temp2 = r;802 }803 }804 let temp3 = parseInt(temp1.value, 16);805 let temp4 = parseInt(temp2.value, 16);806 temp3 = temp3 & temp4;807 console.log(temp3);808 //temp3 = temp4;809 let temp5 = temp3.toString(16);810 let l = temp5.length;811 let valueToStore: string = '';812 for (let i = 0; i < (16 - l); i++) {813 valueToStore = valueToStore + '0';814 }815 valueToStore += temp5;816 temp1.value = valueToStore;817 temp1.arrow = 'fa fa-hand-o-right fa-4x';818 this.outputValue = temp1.value;819 }820 public orNumber(): void {821 for (let r of this.register) {822 r.arrow = '';823 if (this.selectedRegister1 == r.name) {824 let temp1 = parseInt(r.value, 16);825 let temp2 = parseInt(this.valueToAdd, 16);826 temp1 = temp1 | temp2;827 console.log(temp1);828 //append zeros so that length becomes 16829 let temp3 = temp1.toString(16);830 let l = temp3.length;831 let valueToStore: string = '';832 for (var i = 0; i < (16 - l); i++) {833 valueToStore = valueToStore + '0';834 }835 valueToStore = valueToStore + temp3;836 console.log(valueToStore);837 r.value = valueToStore;838 r.arrow = 'fa fa-hand-o-right fa-4x';839 }840 }841 }842 public orRegister(): void {843 let temp1: Register = new Register('', '0', '');844 let temp2: Register = new Register('', '0', '');845 for (let r of this.register) {846 r.arrow = '';847 if (this.selectedRegister2 == r.name) {848 temp1 = r;849 }850 }851 for (let r of this.register) {852 if (this.selectedRegister3 == r.name) {853 temp2 = r;854 }855 }856 let temp3 = parseInt(temp1.value, 16);857 let temp4 = parseInt(temp2.value, 16);858 temp3 = temp3 | temp4;859 console.log(temp3);860 //temp3 = temp4;861 let temp5 = temp3.toString(16);862 let l = temp5.length;863 let valueToStore: string = '';864 for (let i = 0; i < (16 - l); i++) {865 valueToStore = valueToStore + '0';866 }867 valueToStore += temp5;868 temp1.value = valueToStore;869 temp1.arrow = 'fa fa-hand-o-right fa-4x';870 this.outputValue = temp1.value;871 }872 public xorNumber(): void {873 for (let r of this.register) {874 r.arrow = '';875 if (this.selectedRegister1 == r.name) {876 let temp1 = parseInt(r.value, 16);877 let temp2 = parseInt(this.valueToAdd, 16);878 temp1 = temp1 ^ temp2;879 console.log(temp1);880 //append zeros so that length becomes 16881 let temp3 = temp1.toString(16);882 let l = temp3.length;883 let valueToStore: string = '';884 for (var i = 0; i < (16 - l); i++) {885 valueToStore = valueToStore + '0';886 }887 valueToStore = valueToStore + temp3;888 console.log(valueToStore);889 r.value = valueToStore;890 r.arrow = 'fa fa-hand-o-right fa-4x';891 }892 }893 }894 public xorRegister(): void {895 let temp1: Register = new Register('', '0', '');896 let temp2: Register = new Register('', '0', '');897 for (let r of this.register) {898 r.arrow = '';899 if (this.selectedRegister2 == r.name) {900 temp1 = r;901 }902 }903 for (let r of this.register) {904 if (this.selectedRegister3 == r.name) {905 temp2 = r;906 }907 }908 let temp3 = parseInt(temp1.value, 16);909 let temp4 = parseInt(temp2.value, 16);910 temp3 = temp3 ^ temp4;911 console.log(temp3);912 //temp3 = temp4;913 let temp5 = temp3.toString(16);914 let l = temp5.length;915 let valueToStore: string = '';916 for (let i = 0; i < (16 - l); i++) {917 valueToStore = valueToStore + '0';918 }919 valueToStore += temp5;920 temp1.value = valueToStore;921 temp1.arrow = 'fa fa-hand-o-right fa-4x';922 this.outputValue = temp1.value;923 }924 public notNumber(): void {925 for (let r of this.register) {926 r.arrow = '';927 if (this.selectedRegister1 == r.name) {928 let temp1 = parseInt(r.value, 16);929 //let temp2 = parseInt(this.valueToAdd, 16);930 temp1 = ~temp1;931 console.log(temp1);932 //append zeros so that length becomes 16933 let temp3 = temp1.toString(16);934 let l = temp3.length;935 let valueToStore: string = '';936 for (var i = 0; i < (16 - l); i++) {937 valueToStore = valueToStore + '0';938 }939 valueToStore = valueToStore + temp3;940 console.log(valueToStore);941 r.value = valueToStore;942 r.arrow = 'fa fa-hand-o-right fa-4x';943 }944 }945 }946 public showAddSection() {947 this.resetParameters();948 this.showAdd = true;949 }950 public showSubSection() {951 this.resetParameters();952 this.showSub = true;953 }954 public showMulSection() {955 this.resetParameters();956 this.showMul = true;957 }958 public showDivSection() {959 this.resetParameters();960 this.showDiv = true;961 }962 public showMovSection() {963 this.resetParameters();964 this.showMov = true;965 }966 public showIncSection() {967 this.resetParameters();968 this.showInc = true;969 }970 public showDecSection() {971 this.resetParameters();972 this.showDec = true;973 }974 public showPushSection() {975 this.resetParameters();976 this.showPush = true;977 }978 public showPopSection() {979 this.resetParameters();980 this.showPop = true;981 }982 public showAndSection() {983 this.resetParameters();984 this.showAnd = true;985 }986 public showORSection() {987 this.resetParameters();988 this.showOR = true;989 }990 public showXORSection() {991 this.resetParameters();992 this.showXOR = true;993 }994 public showNOTSection() {995 this.resetParameters();996 this.showNOT = true;997 }...
useLocalStorage.ts
Source:useLocalStorage.ts
1import { Dispatch, SetStateAction, useState } from "react";2export type UseLocalStorage<T> = [T, Dispatch<SetStateAction<T>>];3export default function useLocalStorage<T>(4 key: string,5 initialValue: SetStateAction<T>6): UseLocalStorage<T> {7 const saveToLocalStorage = (valueToStore: T) => {8 try {9 if (typeof valueToStore === "string") {10 localStorage.setItem(key, valueToStore);11 } else if (typeof valueToStore === "undefined") {12 localStorage.setItem(key, "");13 } else {14 localStorage.setItem(key, JSON.stringify(valueToStore));15 }16 } catch {17 console.warn(`Could not save ${key} to localStorage`);18 }19 };20 function getValue(value: T, initOrCb: SetStateAction<T>): T {21 if (initOrCb instanceof Function) {22 const newValue = initOrCb(value);23 saveToLocalStorage(newValue);24 return newValue;25 }26 return value ?? initOrCb;27 }28 const [storedValue, setStoredValue] = useState<T>(() => {29 let result: T | null;30 const item = localStorage.getItem(key);31 if (item === null) {32 return getValue(null, initialValue);33 }34 try {35 const parsed = JSON.parse(item);36 if (!parsed) {37 throw new Error("Empty value");38 }39 result = parsed;40 } catch {41 // Casting to T (which should resolve to string) because JSON.parse would42 // throw an error if "foo" was passed, but properly casting "true" or "1"43 // to their respective types44 result = (item as unknown) as T;45 }46 return getValue(result, initialValue);47 });48 const setValue = (value: SetStateAction<T>) => {49 const valueToStore = value instanceof Function ? value(storedValue) : value;50 setStoredValue(valueToStore);51 saveToLocalStorage(valueToStore);52 };53 return [storedValue, setValue];...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log('Test status:', data.statusText);5 console.log('View results:', data.data.userUrl);6});7var wpt = require('webpagetest');8var wpt = new WebPageTest('www.webpagetest.org');9 if (err) return console.error(err);10 console.log('Test status:', data.statusText);11 console.log('View results:', data.data.userUrl);12});13var wpt = require('webpagetest');14var wpt = new WebPageTest('www.webpagetest.org');15 if (err) return console.error(err);16 console.log('Test status:', data.statusText);17 console.log('View results:', data.data.userUrl);18});19var wpt = require('webpagetest');20var wpt = new WebPageTest('www.webpagetest.org');21 if (err) return console.error(err);22 console.log('Test status:', data.statusText);23 console.log('View results:', data.data.userUrl);24});25var wpt = require('webpagetest');26var wpt = new WebPageTest('www.webpagetest.org');27 if (err) return console.error(err);28 console.log('Test status:', data.statusText);29 console.log('
Using AI Code Generation
1var wpt = require('wpt');2wpt.valueToStore('value');3var wpt = require('wpt');4wpt.valueToStore('value');5var wpt = require('wpt');6wpt.valueToStore('value');7var wpt = require('wpt');8wpt.valueToStore('value');9var wpt = require('wpt');10wpt.valueToStore('value');11var wpt = require('wpt');12wpt.valueToStore('value');13var wpt = require('wpt');14wpt.valueToStore('value');15var wpt = require('wpt');16wpt.valueToStore('value');17var wpt = require('wpt');18wpt.valueToStore('value');19var wpt = require('wpt');20wpt.valueToStore('value');21var wpt = require('wpt');22wpt.valueToStore('value');23var wpt = require('wpt');24wpt.valueToStore('value');25var wpt = require('wpt');26wpt.valueToStore('value');27var wpt = require('wpt');28wpt.valueToStore('value');29var wpt = require('wpt');30wpt.valueToStore('value');
Using AI Code Generation
1var wpt = require('wpt');2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7});8var wpt = require('wpt');9 if (err) {10 console.log(err);11 } else {12 console.log(data);13 }14});15var wpt = require('wpt');16 if (err) {17 console.log(err);18 } else {19 console.log(data);20 }21});22var wpt = require('wpt');23 if (err) {24 console.log(err);25 } else {26 console.log(data);27 }28});29var wpt = require('wpt');30 if (err) {31 console.log(err);32 } else {33 console.log(data);34 }35});36var wpt = require('wpt');37 if (err) {38 console.log(err);39 } else {40 console.log(data);41 }42});43var wpt = require('wpt');44 if (err) {45 console.log(err);46 } else {47 console.log(data);48 }49});
Using AI Code Generation
1var wpt = require('./wpt');2var wptObj = new wpt();3wptObj.valueToStore(10);4var wpt = require('./wpt');5var wptObj = new wpt();6wptObj.valueToStore(10);7var wpt = require('./wpt');8var wptObj = new wpt();9wptObj.valueToStore(10);10var wpt = require('./wpt');11var wptObj = new wpt();12wptObj.valueToStore(10);13var wpt = require('./wpt');14var wptObj = new wpt();15wptObj.valueToStore(10);16var wpt = require('./wpt');17var wptObj = new wpt();18wptObj.valueToStore(10);19var wpt = require('./wpt');20var wptObj = new wpt();21wptObj.valueToStore(10);22var wpt = require('./wpt');23var wptObj = new wpt();24wptObj.valueToStore(10);25var wpt = require('./wpt');26var wptObj = new wpt();27wptObj.valueToStore(10);28var wpt = require('./wpt');29var wptObj = new wpt();30wptObj.valueToStore(10);31var wpt = require('./wpt');32var wptObj = new wpt();33wptObj.valueToStore(10);34var wpt = require('./wpt');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!