Repository: denoland/deno
File: cli/tools/repl/session.rs (Rust)

Code before:
use crate::tools::test::TestFailureFormatOptions;

fn comment_source_to_position_range(
  comment_start: SourcePos,
  m: &Match,
  text_info: &SourceTextInfo,
  is_jsx_import_source: bool,
) -> PositionRange {
  // the comment text starts after the double slash or slash star, so add 2
  let comment_start = comment_start + 2;
  // -1 and +1 to include the quotes, but not for jsx import sources because
  // they don't have quotes
  let padding = if is_jsx_import_source { 0 } else { 1 };
  PositionRange {
    start: Position::from_source_pos(
      comment_start + m.start() - padding,
      text_info,
    ),
    end: Position::from_source_pos(
      comment_start + m.end() + padding,
      text_info,
    ),
  }
}

thread_local! {
  /// We store functions used in the repl on this object because
  /// the user might modify the `Deno` global or delete it outright.
  pub static REPL_INTERNAL_OBJECT_ID: RefCell<Option<RemoteObjectId>> = const { RefCell::new(None) };
}
fn get_prelude() -> String {
  r#"(() => {
  const repl_internal = {
      lastEvalResult: undefined,
      lastThrownError: undefined,
      inspectArgs: Deno[Deno.internal].inspectArgs,
      noColor: Deno.noColor,
      get closed() {
        try {
          return typeof globalThis.closed === 'undefined' ? false : globalThis.closed;
        } catch {
          return false;
        }
      }
    }
  Object.defineProperty(globalThis, "_", {
    configurable: true,
    get: () => repl_internal.lastEvalResult,
    set: (value) => {
     Object.defineProperty(globalThis, "_", {
       value: value,

Review comment (suggestion):
I'm not a fan of using a `thread_local` here - consider storing this object id on `ReplSession` object

Code after:

fn comment_source_to_position_range(
  comment_start: SourcePos,
  m: &Match,
  text_info: &SourceTextInfo,
  is_jsx_import_source: bool,
) -> PositionRange {
  // the comment text starts after the double slash or slash star, so add 2
  let comment_start = comment_start + 2;
  // -1 and +1 to include the quotes, but not for jsx import sources because
  // they don't have quotes
  let padding = if is_jsx_import_source { 0 } else { 1 };
  PositionRange {
    start: Position::from_source_pos(
      comment_start + m.start() - padding,
      text_info,
    ),
    end: Position::from_source_pos(
      comment_start + m.end() + padding,
      text_info,
    ),
  }
}

fn get_prelude() -> String {
  r#"(() => {
  const repl_internal = {
      lastEvalResult: undefined,
      lastThrownError: undefined,
      inspectArgs: Deno[Deno.internal].inspectArgs,
      noColor: Deno.noColor,
      get closed() {
        try {
          return typeof globalThis.closed === 'undefined' ? false : globalThis.closed;
        } catch {
          return false;
        }
      }
    }
  Object.defineProperty(globalThis, "_", {
    configurable: true,
    get: () => repl_internal.lastEvalResult,
    set: (value) => {
     Object.defineProperty(globalThis, "_", {
       value: value,
       writable: true,
       enumerable: true,
       configurable: true,
     });
     console.log("Last evaluation result is no longer saved to _.");
    },