or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.mdjava-integration.mdjavascript-objects.mdscript-execution.mdsecurity-debugging.md

javascript-objects.mddocs/

0

# JavaScript Objects

1

2

Complete JavaScript object system implementing all ECMAScript built-in objects including ES6+ features like Promises, Symbols, Maps, Sets, and modern iteration protocols.

3

4

## Capabilities

5

6

### Scriptable Interface

7

8

Core interface that all JavaScript objects must implement, providing the foundation for property access, prototype chains, and object behavior.

9

10

```java { .api }

11

/**

12

* Core interface for all JavaScript objects

13

* Defines property access, prototype chain, and introspection

14

*/

15

public interface Scriptable {

16

/**

17

* Gets a named property from this object

18

* @param name Property name

19

* @param start Object where lookup started (for prototype chain)

20

* @return Property value or NOT_FOUND

21

*/

22

Object get(String name, Scriptable start);

23

24

/**

25

* Gets an indexed property from this object

26

* @param index Property index

27

* @param start Object where lookup started

28

* @return Property value or NOT_FOUND

29

*/

30

Object get(int index, Scriptable start);

31

32

/**

33

* Sets a named property on this object

34

* @param name Property name

35

* @param start Object where lookup started

36

* @param value Property value to set

37

*/

38

void put(String name, Scriptable start, Object value);

39

40

/**

41

* Sets an indexed property on this object

42

* @param index Property index

43

* @param start Object where lookup started

44

* @param value Property value to set

45

*/

46

void put(int index, Scriptable start, Object value);

47

48

/**

49

* Checks if named property exists on this object

50

* @param name Property name

51

* @param start Object where lookup started

52

* @return true if property exists

53

*/

54

boolean has(String name, Scriptable start);

55

56

/**

57

* Checks if indexed property exists on this object

58

* @param index Property index

59

* @param start Object where lookup started

60

* @return true if property exists

61

*/

62

boolean has(int index, Scriptable start);

63

64

/**

65

* Deletes a named property from this object

66

* @param name Property name to delete

67

*/

68

void delete(String name);

69

70

/**

71

* Deletes an indexed property from this object

72

* @param index Property index to delete

73

*/

74

void delete(int index);

75

76

/**

77

* Gets the prototype object for this object

78

* @return Prototype object or null

79

*/

80

Scriptable getPrototype();

81

82

/**

83

* Sets the prototype object for this object

84

* @param prototype New prototype object

85

*/

86

void setPrototype(Scriptable prototype);

87

88

/**

89

* Gets the parent scope for variable resolution

90

* @return Parent scope object or null

91

*/

92

Scriptable getParentScope();

93

94

/**

95

* Sets the parent scope for variable resolution

96

* @param parent New parent scope

97

*/

98

void setParentScope(Scriptable parent);

99

100

/**

101

* Gets the [[Class]] internal property (e.g., "Object", "Array")

102

* @return Class name string

103

*/

104

String getClassName();

105

106

/**

107

* Gets array of enumerable property IDs

108

* @return Array of property names and indices

109

*/

110

Object[] getIds();

111

112

/**

113

* Converts object to primitive value with type hint

114

* @param hint Type hint (String.class, Number.class, etc.)

115

* @return Primitive value

116

*/

117

Object getDefaultValue(Class<?> hint);

118

119

/**

120

* Implements instanceof operator behavior

121

* @param instance Object to test

122

* @return true if this object is in instance's prototype chain

123

*/

124

boolean hasInstance(Scriptable instance);

125

}

126

127

/**

128

* Sentinel value returned when property doesn't exist

129

*/

130

public static final Object NOT_FOUND;

131

```

132

133

**Usage Examples:**

134

135

```java

136

// Basic property access

137

Scriptable obj = cx.newObject(scope);

138

obj.put("name", obj, "example");

139

obj.put("value", obj, 42);

140

141

Object name = obj.get("name", obj); // "example"

142

boolean hasValue = obj.has("value", obj); // true

143

144

// Array-like access

145

obj.put(0, obj, "first");

146

obj.put(1, obj, "second");

147

Object first = obj.get(0, obj); // "first"

148

149

// Prototype chain

150

Scriptable proto = cx.newObject(scope);

151

proto.put("inherited", proto, "value");

152

obj.setPrototype(proto);

153

Object inherited = obj.get("inherited", obj); // "value"

154

```

155

156

### ScriptableObject

157

158

Base implementation of Scriptable with additional functionality for property attributes, sealing, and utility methods.

159

160

```java { .api }

161

/**

162

* Base implementation of Scriptable with enhanced functionality

163

* Provides property attributes, sealing, and utility methods

164

*/

165

public abstract class ScriptableObject implements Scriptable {

166

/**

167

* Gets property from any Scriptable object

168

* @param obj Target object

169

* @param name Property name

170

* @return Property value or NOT_FOUND

171

*/

172

public static Object getProperty(Scriptable obj, String name);

173

174

/**

175

* Gets indexed property from any Scriptable object

176

* @param obj Target object

177

* @param index Property index

178

* @return Property value or NOT_FOUND

179

*/

180

public static Object getProperty(Scriptable obj, int index);

181

182

/**

183

* Sets property on any Scriptable object

184

* @param obj Target object

185

* @param name Property name

186

* @param value Property value

187

*/

188

public static void putProperty(Scriptable obj, String name, Object value);

189

190

/**

191

* Sets indexed property on any Scriptable object

192

* @param obj Target object

193

* @param index Property index

194

* @param value Property value

195

*/

196

public static void putProperty(Scriptable obj, int index, Object value);

197

198

/**

199

* Checks if property exists on any Scriptable object

200

* @param obj Target object

201

* @param name Property name

202

* @return true if property exists

203

*/

204

public static boolean hasProperty(Scriptable obj, String name);

205

206

/**

207

* Deletes property from any Scriptable object

208

* @param obj Target object

209

* @param name Property name to delete

210

* @return true if deletion succeeded

211

*/

212

public static boolean deleteProperty(Scriptable obj, String name);

213

214

/**

215

* Defines a property with value and attributes

216

* @param propertyName Property name

217

* @param value Property value

218

* @param attributes Property attributes (READONLY, DONTENUM, etc.)

219

*/

220

public void defineProperty(String propertyName, Object value, int attributes);

221

222

/**

223

* Defines properties from Java class methods

224

* @param propertyName Property name

225

* @param clazz Java class containing getter/setter methods

226

* @param attributes Property attributes

227

*/

228

public void defineProperty(String propertyName, Class<?> clazz, int attributes);

229

230

/**

231

* Defines multiple function properties from class methods

232

* @param names Array of function names

233

* @param clazz Java class containing methods

234

* @param attributes Property attributes

235

*/

236

public void defineFunctionProperties(String[] names, Class<?> clazz, int attributes);

237

238

/**

239

* Seals this object (prevents property addition/deletion)

240

*/

241

public void sealObject();

242

243

/**

244

* Checks if this object is sealed

245

* @return true if object is sealed

246

*/

247

public boolean isSealed();

248

249

/**

250

* Sets attributes for a property

251

* @param name Property name

252

* @param attributes New attributes

253

*/

254

public void setAttributes(String name, int attributes);

255

256

/**

257

* Gets attributes for a property

258

* @param name Property name

259

* @return Property attributes

260

*/

261

public int getAttributes(String name);

262

}

263

264

// Property attribute constants

265

public static final int READONLY = 1; // Property cannot be modified

266

public static final int DONTENUM = 2; // Property not enumerable

267

public static final int DONTDELETE = 4; // Property cannot be deleted

268

public static final int CONST = 13; // READONLY | DONTENUM | DONTDELETE

269

```

270

271

**Usage Examples:**

272

273

```java

274

// Define properties with attributes

275

ScriptableObject obj = (ScriptableObject) cx.initStandardObjects();

276

obj.defineProperty("PI", Math.PI, ScriptableObject.READONLY | ScriptableObject.DONTENUM);

277

278

// Define function properties from Java methods

279

obj.defineFunctionProperties(new String[]{"myFunction"}, MyClass.class, ScriptableObject.DONTENUM);

280

281

// Seal object to prevent modifications

282

obj.sealObject();

283

boolean sealed = obj.isSealed(); // true

284

285

// Utility methods work on any Scriptable

286

ScriptableObject.putProperty(obj, "temp", "value");

287

Object temp = ScriptableObject.getProperty(obj, "temp");

288

```

289

290

### Core JavaScript Objects

291

292

#### NativeObject

293

294

JavaScript Object implementation providing the base object functionality.

295

296

```java { .api }

297

/**

298

* JavaScript Object implementation

299

* Extends IdScriptableObject for optimized property access

300

*/

301

public class NativeObject extends IdScriptableObject {

302

/**

303

* Default constructor creates empty object

304

*/

305

public NativeObject();

306

}

307

```

308

309

#### NativeArray

310

311

JavaScript Array implementation with full Array.prototype methods and Java List interface.

312

313

```java { .api }

314

/**

315

* JavaScript Array implementation

316

* Implements both Scriptable and java.util.List interfaces

317

*/

318

public class NativeArray extends IdScriptableObject implements List<Object> {

319

/**

320

* Creates array with specified length

321

* @param length Initial array length

322

*/

323

public NativeArray(long length);

324

325

/**

326

* Creates array from Java array elements

327

* @param array Initial elements

328

*/

329

public NativeArray(Object[] array);

330

331

// List interface methods

332

public int size();

333

public boolean isEmpty();

334

public boolean contains(Object o);

335

public Iterator<Object> iterator();

336

public Object[] toArray();

337

public boolean add(Object o);

338

public boolean remove(Object o);

339

public void clear();

340

public Object get(int index);

341

public Object set(int index, Object element);

342

public void add(int index, Object element);

343

public Object remove(int index);

344

}

345

```

346

347

**Usage Examples:**

348

349

```java

350

// Create arrays

351

NativeArray arr1 = new NativeArray(5); // length 5

352

Object[] elements = {"a", "b", "c"};

353

NativeArray arr2 = new NativeArray(elements);

354

355

// Use as Java List

356

List<Object> list = new NativeArray(0);

357

list.add("item1");

358

list.add("item2");

359

System.out.println(list.size()); // 2

360

361

// Access via Scriptable interface

362

arr2.put(0, arr2, "modified");

363

Object first = arr2.get(0, arr2); // "modified"

364

```

365

366

#### NativeFunction

367

368

Base class for all JavaScript functions providing call and construct functionality.

369

370

```java { .api }

371

/**

372

* Base class for JavaScript functions

373

* Extends BaseFunction with additional functionality

374

*/

375

public class NativeFunction extends BaseFunction {

376

// Inherited from Function interface

377

/**

378

* Calls function with specified arguments

379

* @param cx Current Context

380

* @param scope Scope for execution

381

* @param thisObj 'this' object for call

382

* @param args Function arguments

383

* @return Function result

384

*/

385

public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args);

386

387

/**

388

* Constructs new object using function as constructor

389

* @param cx Current Context

390

* @param scope Scope for construction

391

* @param args Constructor arguments

392

* @return New object instance

393

*/

394

public Scriptable construct(Context cx, Scriptable scope, Object[] args);

395

}

396

```

397

398

### Primitive Wrapper Objects

399

400

#### NativeString

401

402

JavaScript String object implementation with all String.prototype methods.

403

404

```java { .api }

405

/**

406

* JavaScript String object implementation

407

* Wraps Java String with JavaScript String prototype methods

408

*/

409

public class NativeString extends IdScriptableObject {

410

/**

411

* Creates String object from Java string

412

* @param s Java string value

413

*/

414

public NativeString(String s);

415

416

/**

417

* Gets the wrapped string value

418

* @return Java String value

419

*/

420

public String getStringValue();

421

}

422

```

423

424

#### NativeNumber

425

426

JavaScript Number object implementation with Number.prototype methods.

427

428

```java { .api }

429

/**

430

* JavaScript Number object implementation

431

* Wraps Java Number with JavaScript Number prototype methods

432

*/

433

public class NativeNumber extends IdScriptableObject {

434

/**

435

* Creates Number object from double value

436

* @param number Numeric value

437

*/

438

public NativeNumber(double number);

439

440

/**

441

* Gets the wrapped numeric value

442

* @return Numeric value as double

443

*/

444

public double getDoubleValue();

445

}

446

```

447

448

#### NativeBoolean

449

450

JavaScript Boolean object implementation.

451

452

```java { .api }

453

/**

454

* JavaScript Boolean object implementation

455

* Wraps Java boolean with JavaScript Boolean prototype methods

456

*/

457

public class NativeBoolean extends IdScriptableObject {

458

/**

459

* Creates Boolean object from boolean value

460

* @param b Boolean value

461

*/

462

public NativeBoolean(boolean b);

463

464

/**

465

* Gets the wrapped boolean value

466

* @return Boolean value

467

*/

468

public boolean getBooleanValue();

469

}

470

```

471

472

#### NativeDate

473

474

JavaScript Date object implementation with full Date API.

475

476

```java { .api }

477

/**

478

* JavaScript Date object implementation

479

* Provides full Date constructor and prototype functionality

480

*/

481

public class NativeDate extends IdScriptableObject {

482

/**

483

* Creates Date object for current time

484

*/

485

public NativeDate();

486

487

/**

488

* Creates Date object from milliseconds since epoch

489

* @param time Milliseconds since January 1, 1970 UTC

490

*/

491

public NativeDate(double time);

492

493

/**

494

* Gets time value as milliseconds since epoch

495

* @return Time in milliseconds

496

*/

497

public double getTime();

498

}

499

```

500

501

### ES6+ Objects

502

503

#### NativeSymbol

504

505

JavaScript Symbol implementation with global symbol registry.

506

507

```java { .api }

508

/**

509

* JavaScript Symbol implementation

510

* Provides Symbol constructor and global registry

511

*/

512

public class NativeSymbol extends IdScriptableObject {

513

/**

514

* Creates or retrieves global symbol for key

515

* @param key Symbol key string

516

* @return Global symbol for key

517

*/

518

public static Symbol for(String key);

519

520

/**

521

* Gets key for global symbol

522

* @param symbol Symbol to look up

523

* @return Key string or null if not global symbol

524

*/

525

public static String keyFor(Symbol symbol);

526

}

527

528

/**

529

* Symbol primitive value

530

*/

531

public class Symbol {

532

/**

533

* Gets string representation of symbol

534

* @return String representation

535

*/

536

public String toString();

537

}

538

```

539

540

#### NativePromise

541

542

JavaScript Promise implementation with full ES6 Promise API.

543

544

```java { .api }

545

/**

546

* JavaScript Promise implementation

547

* Provides full Promise constructor and prototype methods

548

*/

549

public class NativePromise extends IdScriptableObject {

550

/**

551

* Creates resolved Promise with value

552

* @param cx Current Context

553

* @param scope Current scope

554

* @param value Resolution value

555

* @return Resolved Promise

556

*/

557

public static NativePromise resolve(Context cx, Scriptable scope, Object value);

558

559

/**

560

* Creates rejected Promise with reason

561

* @param cx Current Context

562

* @param scope Current scope

563

* @param reason Rejection reason

564

* @return Rejected Promise

565

*/

566

public static NativePromise reject(Context cx, Scriptable scope, Object reason);

567

568

/**

569

* Creates Promise that resolves when all input promises resolve

570

* @param cx Current Context

571

* @param scope Current scope

572

* @param promises Array of promises

573

* @return Promise that resolves to array of results

574

*/

575

public static NativePromise all(Context cx, Scriptable scope, Object promises);

576

}

577

```

578

579

#### NativeMap and NativeSet

580

581

ES6 Map and Set implementations with iteration support.

582

583

```java { .api }

584

/**

585

* JavaScript Map implementation

586

* Provides Map constructor and prototype methods

587

*/

588

public class NativeMap extends IdScriptableObject {

589

/**

590

* Creates new empty Map

591

*/

592

public NativeMap();

593

594

/**

595

* Gets value for key

596

* @param key Map key

597

* @return Value or undefined

598

*/

599

public Object get(Object key);

600

601

/**

602

* Sets key-value pair

603

* @param key Map key

604

* @param value Map value

605

* @return This Map for chaining

606

*/

607

public NativeMap set(Object key, Object value);

608

609

/**

610

* Checks if key exists

611

* @param key Key to check

612

* @return true if key exists

613

*/

614

public boolean has(Object key);

615

616

/**

617

* Deletes key-value pair

618

* @param key Key to delete

619

* @return true if key existed and was deleted

620

*/

621

public boolean delete(Object key);

622

623

/**

624

* Gets number of key-value pairs

625

* @return Size of map

626

*/

627

public int size();

628

}

629

630

/**

631

* JavaScript Set implementation

632

* Provides Set constructor and prototype methods

633

*/

634

public class NativeSet extends IdScriptableObject {

635

/**

636

* Creates new empty Set

637

*/

638

public NativeSet();

639

640

/**

641

* Adds value to set

642

* @param value Value to add

643

* @return This Set for chaining

644

*/

645

public NativeSet add(Object value);

646

647

/**

648

* Checks if value exists in set

649

* @param value Value to check

650

* @return true if value exists

651

*/

652

public boolean has(Object value);

653

654

/**

655

* Deletes value from set

656

* @param value Value to delete

657

* @return true if value existed and was deleted

658

*/

659

public boolean delete(Object value);

660

661

/**

662

* Gets number of values in set

663

* @return Size of set

664

*/

665

public int size();

666

}

667

```

668

669

### Iteration Protocol

670

671

#### ES6Iterator

672

673

Base class for all ES6-style iterators supporting for-of loops.

674

675

```java { .api }

676

/**

677

* Base class for ES6 iterators

678

* Implements iterator protocol for for-of loops

679

*/

680

public abstract class ES6Iterator extends IdScriptableObject {

681

/**

682

* Gets next iteration result

683

* @return Object with 'value' and 'done' properties

684

*/

685

public abstract Object next();

686

687

/**

688

* Makes object iterable (returns itself)

689

* @return This iterator

690

*/

691

public ES6Iterator iterator();

692

}

693

694

/**

695

* Array iterator for Array.prototype[Symbol.iterator]

696

*/

697

public class NativeArrayIterator extends ES6Iterator {

698

/**

699

* Creates iterator for array

700

* @param array Array to iterate

701

* @param type Iteration type (keys, values, entries)

702

*/

703

public NativeArrayIterator(Scriptable array, Type type);

704

705

public enum Type {

706

KEYS, // Iterate indices

707

VALUES, // Iterate values

708

ENTRIES // Iterate [index, value] pairs

709

}

710

}

711

712

/**

713

* String iterator for String.prototype[Symbol.iterator]

714

*/

715

public class NativeStringIterator extends ES6Iterator {

716

/**

717

* Creates iterator for string

718

* @param string String to iterate (by Unicode code points)

719

*/

720

public NativeStringIterator(String string);

721

}

722

```

723

724

### Special Objects

725

726

#### NativeError

727

728

JavaScript Error object with stack trace support.

729

730

```java { .api }

731

/**

732

* JavaScript Error object implementation

733

* Supports Error constructor and stack traces

734

*/

735

public class NativeError extends IdScriptableObject {

736

/**

737

* Creates Error with message

738

* @param message Error message

739

*/

740

public NativeError(String message);

741

742

/**

743

* Gets error message

744

* @return Error message string

745

*/

746

public String getMessage();

747

748

/**

749

* Gets stack trace

750

* @return Stack trace string

751

*/

752

public String getStackTrace();

753

}

754

```

755

756

#### NativeJSON

757

758

JavaScript JSON object with parse/stringify methods.

759

760

```java { .api }

761

/**

762

* JavaScript JSON object implementation

763

* Provides JSON.parse and JSON.stringify functionality

764

*/

765

public class NativeJSON extends IdScriptableObject {

766

/**

767

* Parses JSON string into JavaScript value

768

* @param cx Current Context

769

* @param scope Current scope

770

* @param text JSON string to parse

771

* @param reviver Optional reviver function

772

* @return Parsed JavaScript value

773

*/

774

public static Object parse(Context cx, Scriptable scope, String text, Function reviver);

775

776

/**

777

* Converts JavaScript value to JSON string

778

* @param cx Current Context

779

* @param scope Current scope

780

* @param value Value to stringify

781

* @param replacer Optional replacer function/array

782

* @param space Optional indentation

783

* @return JSON string representation

784

*/

785

public static String stringify(Context cx, Scriptable scope, Object value, Object replacer, Object space);

786

}

787

```

788

789

**Usage Examples:**

790

791

```java

792

// Working with native objects

793

Scriptable obj = cx.newObject(scope);

794

ScriptableObject.putProperty(obj, "name", "test");

795

796

// Arrays with List interface

797

NativeArray arr = new NativeArray(new Object[]{"a", "b", "c"});

798

arr.add("d"); // Use as Java List

799

Object item = arr.get(1, arr); // Use as Scriptable

800

801

// ES6 collections

802

NativeMap map = new NativeMap();

803

map.set("key1", "value1");

804

map.set("key2", "value2");

805

System.out.println(map.size()); // 2

806

807

NativeSet set = new NativeSet();

808

set.add("item1");

809

set.add("item2");

810

boolean hasItem = set.has("item1"); // true

811

812

// Promises (basic example)

813

NativePromise resolved = NativePromise.resolve(cx, scope, "success");

814

NativePromise rejected = NativePromise.reject(cx, scope, "error");

815

816

// JSON operations

817

String json = "{\"name\":\"test\",\"value\":42}";

818

Object parsed = NativeJSON.parse(cx, scope, json, null);

819

String serialized = NativeJSON.stringify(cx, scope, parsed, null, null);

820

```