registerGlobalVariables static method

void registerGlobalVariables(
  1. D4rt interpreter,
  2. String importPath, {
  3. bool include(
    1. String? sourceUri
    )?,
})

Registers all global variables with the interpreter.

importPath is the package import path for library-scoped registration. Collects all registration errors and throws a single exception with all error details if any registrations fail.

Implementation

static void registerGlobalVariables(
  D4rt interpreter,
  String importPath, {
  bool Function(String? sourceUri)? include,
}) {
  final errors = <String>[];

  if (include == null || include('package:path/path.dart')) {
    try {
      interpreter.registerGlobalVariable(
        'posix',
        $path_1.posix,
        importPath,
        sourceUri: 'package:path/path.dart',
      );
    } catch (e) {
      errors.add('Failed to register variable "posix": $e');
    }
  }
  if (include == null || include('package:path/path.dart')) {
    try {
      interpreter.registerGlobalVariable(
        'windows',
        $path_1.windows,
        importPath,
        sourceUri: 'package:path/path.dart',
      );
    } catch (e) {
      errors.add('Failed to register variable "windows": $e');
    }
  }
  if (include == null || include('package:path/path.dart')) {
    try {
      interpreter.registerGlobalVariable(
        'url',
        $path_1.url,
        importPath,
        sourceUri: 'package:path/path.dart',
      );
    } catch (e) {
      errors.add('Failed to register variable "url": $e');
    }
  }
  if (include == null || include('package:path/path.dart')) {
    try {
      interpreter.registerGlobalVariable(
        'context',
        $path_1.context,
        importPath,
        sourceUri: 'package:path/path.dart',
      );
    } catch (e) {
      errors.add('Failed to register variable "context": $e');
    }
  }
  if (include == null || include('package:path/path.dart')) {
    interpreter.registerGlobalGetter(
      'style',
      () => $path_1.style,
      importPath,
      sourceUri: 'package:path/path.dart',
    );
  }
  if (include == null || include('package:path/path.dart')) {
    interpreter.registerGlobalGetter(
      'current',
      () => $path_1.current,
      importPath,
      sourceUri: 'package:path/path.dart',
    );
  }
  if (include == null || include('package:path/path.dart')) {
    interpreter.registerGlobalGetter(
      'separator',
      () => $path_1.separator,
      importPath,
      sourceUri: 'package:path/path.dart',
    );
  }

  if (errors.isNotEmpty) {
    throw StateError(
      'Bridge registration errors (path):\n${errors.join("\n")}',
    );
  }
}