starlark 0.13.0

An implementation of the Starlark language in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * Copyright 2019 The Starlark in Rust Authors.
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::mpsc::channel;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::sync::Mutex;

use debugserver_types::*;
use dupe::Dupe;
use starlark_syntax::error::StarlarkResultExt;
use starlark_syntax::slice_vec_ext::SliceExt;

use super::EvaluateExprInfo;
use super::InspectVariableInfo;
use super::PathSegment;
use super::VariablePath;
use crate::codemap::FileSpan;
use crate::codemap::FileSpanRef;
use crate::codemap::Span;
use crate::debug::adapter::Breakpoint;
use crate::debug::adapter::ResolvedBreakpoints;
use crate::debug::DapAdapter;
use crate::debug::DapAdapterClient;
use crate::debug::DapAdapterEvalHook;
use crate::debug::ScopesInfo;
use crate::debug::StepKind;
use crate::debug::Variable;
use crate::debug::VariablesInfo;
use crate::eval::BeforeStmtFuncDyn;
use crate::eval::Evaluator;
use crate::syntax::AstModule;
use crate::syntax::Dialect;
use crate::values::Value;

pub(crate) fn prepare_dap_adapter(
    client: Box<dyn DapAdapterClient>,
) -> (impl DapAdapter, impl DapAdapterEvalHook) {
    let (sender, receiver) = std::sync::mpsc::channel::<ToEvalMessage>();
    let state = Arc::new(SharedAdapterState {
        client,
        breakpoints: Arc::new(Mutex::new(BreakpointConfig::new())),
        disable_breakpoints: Arc::new(0usize.into()),
    });

    (
        DapAdapterImpl {
            state: state.clone(),
            sender,
        },
        DapAdapterEvalHookImpl::new(state, receiver),
    )
}

type ToEvalMessage = Box<dyn Fn(FileSpanRef, &mut Evaluator) -> Next + Send>;

/// The DapAdapter allows
#[derive(Debug)]
struct DapAdapterImpl {
    state: Arc<SharedAdapterState>,
    sender: Sender<ToEvalMessage>,
}

struct DapAdapterEvalHookImpl {
    state: Arc<SharedAdapterState>,
    receiver: Receiver<ToEvalMessage>,
    step: Option<(StepKind, usize)>,
}

fn evaluate_expr<'v>(
    state: &SharedAdapterState,
    eval: &mut Evaluator<'v, '_, '_>,
    expr: String,
) -> anyhow::Result<Value<'v>> {
    // We don't want to trigger breakpoints during an evaluate,
    // not least because we currently don't allow reenterant evaluate
    state.disable_breakpoints.fetch_add(1, Ordering::SeqCst);
    // Don't use `?`, we need to reset disable_breakpoints.
    let ast = AstModule::parse("interactive", expr, &Dialect::AllOptionsInternal);
    // This technically loses structured access to the diagnostic information. However, it's
    // completely unused, so there's not much point in converting all of this code to using
    // `starlark::Error`, only for buck2 to then go and blindly turn it into a `anyhow::Error`
    // anyway.
    let res = ast
        .and_then(|ast| eval.eval_statements(ast))
        .into_anyhow_result();
    state.disable_breakpoints.fetch_sub(1, Ordering::SeqCst);
    res
}

impl<'a, 'e: 'a> BeforeStmtFuncDyn<'a, 'e> for DapAdapterEvalHookImpl {
    fn call<'v>(
        &mut self,
        span_loc: FileSpanRef,
        eval: &mut Evaluator<'v, 'a, 'e>,
    ) -> crate::Result<()> {
        let stop = if self.state.disable_breakpoints.load(Ordering::SeqCst) > 0 {
            false
        } else {
            let breaks = self.state.breakpoints.lock().unwrap();
            let breakpoint = breaks.at(span_loc);
            match breakpoint {
                Some(Breakpoint {
                    condition: Some(condition),
                    ..
                }) => match evaluate_expr(&self.state, eval, condition.to_owned()) {
                    Ok(v) => v.to_bool(),
                    Err(_) => {
                        // If failed to evaluate the condition, stop.
                        // TODO(nga): print the error.
                        true
                    }
                },
                Some(..) => true,
                None => false,
            }
        };

        let step_stop = match self.step {
            None => false,
            Some((StepKind::Into, _)) => true,
            // These aren't quite right because we only get called before statements and so we could
            // return from the current function and be in an expression that then calls another function
            // without hitting a new statement in the outer function.
            Some((StepKind::Over, stack_size)) => eval.call_stack_count() <= stack_size,
            Some((StepKind::Out, stack_size)) => eval.call_stack_count() < stack_size,
        };

        if stop || step_stop {
            self.step = None;
            self.state.client.event_stopped()?;
            loop {
                let msg = self.receiver.recv();
                match msg.map(|msg| msg(span_loc, eval)) {
                    Ok(Next::Continue) => break,
                    Ok(Next::Step(kind)) => {
                        self.step = Some((kind, eval.call_stack_count()));
                        break;
                    }
                    Ok(Next::RemainPaused) => continue,
                    Err(..) => {
                        // DapAdapter has been dropped so we'll continue.
                        break;
                    }
                }
            }
        }
        Ok(())
    }
}

impl Debug for DapAdapterEvalHookImpl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DapAdapterEvaluationWrapper").finish()
    }
}

impl DapAdapterEvalHookImpl {
    fn new(state: Arc<SharedAdapterState>, receiver: Receiver<ToEvalMessage>) -> Self {
        Self {
            state,
            receiver,
            step: None,
        }
    }
}

impl DapAdapterEvalHook for DapAdapterEvalHookImpl {
    fn add_dap_hooks(self: Box<Self>, eval: &mut Evaluator<'_, '_, '_>) {
        eval.before_stmt_for_dap((self as Box<dyn BeforeStmtFuncDyn>).into());
    }
}

#[derive(Debug)]
struct BreakpointConfig {
    // maps a source filename to the breakpoint spans for the file
    breakpoints: HashMap<String, HashMap<Span, Breakpoint>>,
}

impl BreakpointConfig {
    fn new() -> Self {
        Self {
            breakpoints: HashMap::new(),
        }
    }

    fn at(&self, span_loc: FileSpanRef) -> Option<&Breakpoint> {
        self.breakpoints
            .get(span_loc.filename())
            .and_then(|file_breaks| file_breaks.get(&span_loc.span))
    }

    fn set_breakpoints(
        &mut self,
        source: &str,
        breakpoints: &ResolvedBreakpoints,
    ) -> anyhow::Result<()> {
        if breakpoints.0.is_empty() {
            self.breakpoints.remove(source);
        } else {
            self.breakpoints.insert(
                source.to_owned(),
                breakpoints
                    .0
                    .iter()
                    .filter_map(|x| x.clone())
                    .map(|x| (x.span.span, x))
                    .collect(),
            );
        }
        Ok(())
    }
}

#[derive(Debug)]
struct SharedAdapterState {
    client: Box<dyn DapAdapterClient>,
    // These breakpoints must all match statements as per before_stmt.
    // Those values for which we abort the execution.
    breakpoints: Arc<Mutex<BreakpointConfig>>,
    // Set while we are doing evaluate calls (>= 1 means disable)
    disable_breakpoints: Arc<AtomicUsize>,
}

#[derive(Debug, Clone, Copy, Dupe)]
enum Next {
    Continue,
    RemainPaused,
    Step(StepKind),
}

fn convert_frame(id: usize, name: String, location: Option<FileSpan>) -> StackFrame {
    let mut s = StackFrame {
        id: id as i64,
        name,
        column: 0,
        line: 0,
        end_column: None,
        end_line: None,
        module_id: None,
        presentation_hint: None,
        source: None,
    };
    if let Some(loc) = location {
        let span = loc.resolve_span();
        s.line = span.begin.line as i64 + 1;
        s.column = span.begin.column as i64 + 1;
        s.end_line = Some(span.end.line as i64 + 1);
        s.end_column = Some(span.end.column as i64 + 1);
        s.source = Some(Source {
            path: Some(loc.filename().to_owned()),
            ..Source::default()
        })
    }
    s
}

impl DapAdapter for DapAdapterImpl {
    fn set_breakpoints(
        &self,
        source: &str,
        breakpoints: &ResolvedBreakpoints,
    ) -> anyhow::Result<()> {
        self.state
            .breakpoints
            .lock()
            .unwrap()
            .set_breakpoints(source, breakpoints)
    }

    fn top_frame(&self) -> anyhow::Result<Option<StackFrame>> {
        self.with_ctx(Box::new(|span, eval| {
            let frame = eval.call_stack_top_frame();
            let name = frame.map_or("".to_owned(), |v| v.name);
            Ok(Some(convert_frame(0, name, Some(span.to_file_span()))))
        }))
    }

    fn stack_trace(&self, _: StackTraceArguments) -> anyhow::Result<StackTraceResponseBody> {
        // Our model of a Frame and the debugger model are a bit different.
        // We record the location of the call, but DAP wants the location we are at.
        // We also have them in the wrong order
        self.with_ctx(Box::new(|span, eval| {
            let frames = eval.call_stack().into_frames();
            let mut next = Some(span.to_file_span());
            let mut res = Vec::with_capacity(frames.len() + 1);
            for (i, x) in frames.iter().rev().enumerate() {
                res.push(convert_frame(i, x.name.clone(), next));
                next = x.location.dupe();
            }
            res.push(convert_frame(frames.len(), "Root".to_owned(), next));
            Ok(StackTraceResponseBody {
                total_frames: Some(res.len() as i64),
                stack_frames: res,
            })
        }))
    }

    fn scopes(&self) -> anyhow::Result<ScopesInfo> {
        self.with_ctx(Box::new(|_, eval| {
            let vars = eval.local_variables();
            Ok(ScopesInfo {
                num_locals: vars.len(),
            })
        }))
    }

    fn variables(&self) -> anyhow::Result<VariablesInfo> {
        self.with_ctx(Box::new(|_, eval| {
            let vars = eval.local_variables();
            Ok(VariablesInfo {
                locals: vars
                    .into_iter()
                    .map(|(name, value)| Variable::from_value(PathSegment::Attr(name), value))
                    .collect(),
            })
        }))
    }

    fn inspect_variable(&self, path: VariablePath) -> anyhow::Result<InspectVariableInfo> {
        let state = self.state.dupe();
        self.with_ctx(Box::new(move |_span, eval| {
            let access_path = &path.access_path;
            let mut value = match &path.scope {
                super::Scope::Local(name) => {
                    let mut vars = eval.local_variables();
                    // since vars is owned within this closure scope we can just remove value from the map
                    // obtaining owned variable as the rest of the map will be dropped anyway
                    vars.shift_remove(name).ok_or_else(|| {
                        anyhow::Error::msg(format!("Local variable {} not found", name))
                    })
                }
                super::Scope::Expr(expr) => evaluate_expr(&state, eval, expr.to_owned()),
            }?;

            for p in access_path.iter() {
                value = p.get(&value, eval.heap()).into_anyhow_result()?;
            }
            InspectVariableInfo::try_from_value(value, eval.heap()).into_anyhow_result()
        }))
    }

    fn continue_(&self) -> anyhow::Result<()> {
        self.inject_next(Next::Continue);
        Ok(())
    }

    fn step(&self, kind: StepKind) -> anyhow::Result<()> {
        self.inject_next(Next::Step(kind));
        Ok(())
    }

    fn evaluate(&self, expr: &str) -> anyhow::Result<EvaluateExprInfo> {
        let state = self.state.dupe();
        let expression = expr.to_owned();
        self.with_ctx(Box::new(move |_, eval| {
            match evaluate_expr(&state, eval, expression.clone()) {
                Err(e) => Err(e),
                Ok(v) => Ok(EvaluateExprInfo::from_value(&v)),
            }
        }))
    }
}

impl DapAdapterImpl {
    fn inject<T: 'static + Send>(
        &self,
        f: Box<dyn Fn(FileSpanRef, &mut Evaluator) -> (Next, T) + Send>,
    ) -> T {
        let (sender, receiver) = channel();
        self.sender
            .send(Box::new(move |span, eval| {
                let (next, res) = f(span, eval);
                sender.send(res).unwrap();
                next
            }))
            .unwrap();
        receiver.recv().unwrap()
    }

    fn inject_next(&self, next: Next) {
        self.inject(Box::new(move |_, _| (next, ())))
    }

    fn with_ctx<T: 'static + Send>(
        &self,
        f: Box<dyn Fn(FileSpanRef, &mut Evaluator) -> T + Send>,
    ) -> T {
        self.inject(Box::new(move |span, eval| {
            (Next::RemainPaused, f(span, eval))
        }))
    }
}

pub(crate) fn breakpoint(verified: bool) -> debugserver_types::Breakpoint {
    debugserver_types::Breakpoint {
        column: None,
        end_column: None,
        end_line: None,
        id: None,
        line: None,
        message: None,
        source: None,
        verified,
    }
}

pub(crate) fn resolve_breakpoints(
    args: &SetBreakpointsArguments,
    ast: &AstModule,
) -> anyhow::Result<ResolvedBreakpoints> {
    let poss: HashMap<usize, FileSpan> = ast
        .stmt_locations()
        .iter()
        .map(|span| (span.resolve_span().begin.line, span.dupe()))
        .collect();
    Ok(ResolvedBreakpoints(args.breakpoints.as_ref().map_or(
        Vec::new(),
        |v| {
            v.map(|x| {
                poss.get(&(x.line as usize - 1)).map(|span| Breakpoint {
                    span: span.clone(),
                    condition: x.condition.clone(),
                })
            })
        },
    )))
}

pub(crate) fn resolved_breakpoints_to_dap(
    breakpoints: &ResolvedBreakpoints,
) -> SetBreakpointsResponseBody {
    SetBreakpointsResponseBody {
        breakpoints: breakpoints.0.map(|x| breakpoint(x.is_some())),
    }
}